angular.module("TheNG",[]).controller("AController",function($scope){$scope.someAction=function(){// handle some logic hereserviceLoadedFromExternal.makeServiceCall();};});
使用封装成service的第三方服务
1234567891011121314
angular.module("TheNG",[]).factory("wrappedService",function(){return{makeServiceCall:function(){serviceLoadedFromExternal.makeServiceCall();}};}).controller("AController",function($scope,wrappedService){$scope.someAction=function(){// handle some logic herewrappedService.makeServiceCall();};});
functiongreet(timeFrame,msg){return"Good "+timeFrame+", "+msg;}console.log(greet("Morning","What can I do for you?"));console.log(greet("Morning","This is your ticket"));console.log(greet("Morning","welcome back"));console.log(greet("Morning","How was the trip"));console.log(greet("Afternoon","Can I borrow you some time?"));// "Good Morning, What can I do for you?"// "Good Morning, This is your ticket"// "Good Morning, welcome back"// "Good Morning, How was the trip"// "Good Afternoon, Can I borrow you some time?"
functiongreet(timeFrame,msg){return"Good "+timeFrame+", "+msg;}varmorningGreet=greet.bind(undefined,"Morning");console.log(morningGreet("What can I do for you?"));console.log(morningGreet("This is your ticket"));console.log(morningGreet("welcome back"));console.log(morningGreet("How was the trip"));console.log(greet("Afternoon","Can I borrow you some time?"));
vartodoItem=newTodoItem({"newAttr":"valueOfNewAttr","title":"Build a time machine"});console.log(JSON.stringify(todoItem.toJSON()));// {"newAttr":"valueOfNewAttr","title":"Build a time machine","completed":false}
TodoItem=Backbone.Model.extend({defaults:{title:'',completed:false},validate:function(attributes,options){if(attributes.title.indexOf("<")!=-1){return"html tag is now allowed is title"}}});vartodoItem=newTodoItem({"title":"<script>...</script>"});console.log(todoItem.isValid());// false
smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbu_a\n5.7.14 jp4-nDDfH6fUTS8dQUeQfvPLhoMiYxCCT4CThSQuhcOJbzMUYI_QQZ44jUTB6FbPnYp8gv\n5.7.14 DEXJItt2gSz6WypV6cr7cZv9rpEprruo_JHXIBw6ZK3wjDKeSKKUKYMtpKoJUmARwONwcJ\n5.7.14 D3v5xypcAJtcazDB_WIUNCP8b3ZQl94GOTIKRvr7ASIgNuyD-rud8doBOTRnKpnLHbuc9B\n5.7.14 lZEGyzw> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 hv7sm4775459pdb.86 - gsmtp')
<divng-controller="FruitController"ng-init="init()"><h4>Call controller method via isolated scope</h4><label>What's your favorite fruit(name can only contains letter)</label><inputtype="text"ng-model="newFruit"/><buttontype="button"add-fruit-method-isolatedis-valid="isValid(name)"new-fruit="newFruit"fruits="fruits"> validate and add
</button><ul><ling-repeat="fruit in fruits track by $index"></li></ul></div>
Now comes the final one, we have a reference to the validation method in isolated scope, we want to verify this reference has been called, and with the right arguments.
describe('directives',function(){beforeEach(module('myApp.directives'));describe('addFruitMethodIsolated',function(){var$scope,element;beforeEach(inject(function($rootScope,$compile){$scope=$rootScope;$scope.fruits=[];$scope.newFruit='apple';$scope.isValid=angular.noop;element=angular.element('<input type="text" ng-model="newFruit"/>'+'<button type="button" '+'add-fruit-method-isolated '+'is-valid="isValid(name)" '+'new-fruit="newFruit" '+'fruits="fruits">'+'validate and add</button>');$compile(element)($scope);}));it('should add valid fruit to fruit list when click button',function(){varisValid=spyOn($scope,'isValid').andReturn(true);element.filter('button').trigger('click');expect(isValid).toHaveBeenCalled();expect(isValid.mostRecentCall.args[0]).toBe('apple');expect($scope.fruits[0]).toBe('apple');});});});
Not like verify data change in isolated scope, we don’t need to track method on isolated scope, track on method in default scope is enough. One thing interesting is in the implementation, we need to pass a json object as argument(scope.isValid({name: scope.newFruit})), the json object is used internally in angular, in the test, what we need to verify is only values(expect(isValid.mostRecentCall.args[0]).toBe('apple');).