Angular开发者指南(四)控制器
了解控制器controller
不要使用控制器:
设置$scope对象的初始状态 var myApp = angular.module('myApp',[]); myApp.controller('GreetingController',['$scope',function($scope) { $scope.greeting = 'Hola!'; }]); 我们为我们的应用程序创建一个AngularJS模块myApp。 然后我们使用.controller()方法将控制器的构造函数添加到模块中。 这将使控制器的构造函数保持在全局范围之外。 <div ng-controller="GreetingController"> {{ greeting }} </div> 将行为添加到scope对象 var myApp = angular.module('myApp',[]); myApp.controller('DoubleController',function($scope) { $scope.double = function(value) { return value * 2; }; }]); 将控制器附加到DOM后,可以在模板中的AngularJS表达式中调用double方法: <div ng-controller="DoubleController"> Two times <input ng-model="num"> equals {{ double(num) }} </div> 分配给scope的任何对象(或基元)都将成为模型属性。 分配给作用域的任何方法都在模板/视图中可用,并且可以通过AngularJS表达式和ng事件处理程序指令(例如ngClick)调用。
我们的模板中的消息包含对香料模型的绑定,默认情况下,它设置为字符串“very”。 根据单击哪个按钮,香料模型设置为辣椒或jalape?o,并且消息通过数据绑定自动更新。
<div ng-controller="SpicyController"> <button ng-click="chiliSpicy()">Chili</button> <button ng-click="jalapenoSpicy()">Jalape?o</button> <p>The food is {{spice}} spicy!</p> </div>
var myApp = angular.module('spicyApp1',[]); myApp.controller('SpicyController',function($scope) { $scope.spice = 'very'; $scope.chiliSpicy = function() { $scope.spice = 'chili'; }; $scope.jalapenoSpicy = function() { $scope.spice = 'jalape?o'; }; }]); 在上面的例子注意事项:
控制器方法也可以接受参数,如前面示例的以下变体所示。
<div ng-controller="SpicyController"> <input ng-model="customSpice"> <button ng-click="spicy('chili')">Chili</button> <button ng-click="spicy(customSpice)">Custom spice</button> <p>The food is {{spice}} spicy!</p> </div>
var myApp = angular.module('spicyApp2',function($scope) { $scope.customSpice = 'wasabi'; $scope.spice = 'very'; $scope.spicy = function(spice) { $scope.spice = spice; }; }]); SpicyController控制器现在只定义一个名为spicy的方法,它接受一个名为spice的参数。 模板然后引用此Controller方法,并在第一个按钮的绑定中传递字符串常量“chili”,并在第二个按钮中传递模型属性customSpice(绑定到输入框)。
<div class="spicy"> <div ng-controller="MainController"> <p>Good {{timeOfDay}},{{name}}!</p> <div ng-controller="ChildController"> <p>Good {{timeOfDay}},{{name}}!</p> <div ng-controller="GrandChildController"> <p>Good {{timeOfDay}},{{name}}!</p> </div> </div> </div> </div>
div.spicy div { padding: 10px; border: solid 2px blue; }
var myApp = angular.module('scopeInheritance',[]); myApp.controller('MainController',function($scope) { $scope.timeOfDay = 'morning'; $scope.name = 'Nikki'; }]); myApp.controller('ChildController',function($scope) { $scope.name = 'Mattie'; }]); myApp.controller('GrandChildController',function($scope) { $scope.timeOfDay = 'evening'; $scope.name = 'Gingerbread Baby'; }]); 注意我们如何在模板中嵌套三个ng-controller指令。 这将导致为我们的视图创建四个范围:
继承以与对属性的方式相同的方式处理方法。 所以在我们以前的例子中,所有的属性都可以替换为返回字符串值的方法。
var myApp = angular.module('myApp',[]); myApp.controller('MyController',function($scope) { $scope.spices = [{"name":"pasilla","spiciness":"mild"},{"name":"jalapeno","spiciness":"hot hot hot!"},{"name":"habanero","spiciness":"LAVA HOT!!"}]; $scope.spice = "habanero"; });
describe('myController function',function() { describe('myController',function() { var $scope; beforeEach(module('myApp')); beforeEach(inject(function($rootScope,$controller) { $scope = $rootScope.$new(); $controller('MyController',{$scope: $scope}); })); it('should create "spices" model with 3 spices',function() { expect($scope.spices.length).toBe(3); }); it('should set the default value of spice',function() { expect($scope.spice).toBe('habanero'); }); }); }); 如果需要测试嵌套的控制器,则必须在DOM中存在的测试中创建相同的作用域层次结构: describe('state',function() { var mainScope,childScope,grandChildScope; beforeEach(module('myApp')); beforeEach(inject(function($rootScope,$controller) { mainScope = $rootScope.$new(); $controller('MainController',{$scope: mainScope}); childScope = mainScope.$new(); $controller('ChildController',{$scope: childScope}); grandChildScope = childScope.$new(); $controller('GrandChildController',{$scope: grandChildScope}); })); it('should have over and selected',function() { expect(mainScope.timeOfDay).toBe('morning'); expect(mainScope.name).toBe('Nikki'); expect(childScope.timeOfDay).toBe('morning'); expect(childScope.name).toBe('Mattie'); expect(grandChildScope.timeOfDay).toBe('evening'); expect(grandChildScope.name).toBe('Gingerbread Baby'); }); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |