angular学习(三)—— Controller
Controller介绍 当controller通过ng-controller directive与DOM关联,angular将用指定的controller构造函数实例化一个新的controller对象,同时一个新的child scope将被创建,然后以参数$scope注入到controller中。 如果controller使用了controller as a语法,那么控制器实例将会分配给这个属性a。在第一章有讲到这种情况,可以返回去看下,这里就不再写示例了。 使用controller的情况:
不使用controller的情况:
scope对象初始化 下面的例子演示如何创建一个controller,并且初始化scope对象。 var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController',['$scope',function ($scope) {
$scope.greeting = 'Hola!';
}]);
我们创建了一个angular module : myApp,然后调用module的controller方法,给module增加了一个controller构造函数。 再看下controller相对应的DOM,greeting属性做了数据绑定,可以显示在controller中的值。 <div ng-controller="GreetingController"> {{ greeting }} </div>
给scope对象增加一些行为 下面的示例演示如何为controller增加方法 var myApp = angular.module('myApp',[]);
myApp.controller('DoubleController',function($scope) {
$scope.double = function(value) { return value * 2; };
}]);
controller一旦被添加到DOM中,该方法就可以在template中被调用 <div ng-controller="DoubleController"> Two times <input ng-model="num"> equals {{ double(num) }} </div>
Controller示例
在模板中的消息包含一个spice model,默认设置为very,点击按钮时给spice赋值,通过data binding的方式自动更新这条消息。 <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>
app.js 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';
};
}]);
输出结果
带参数的Controller方法示例 index.html <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>
app.js var myApp = angular.module('spicyApp2',function($scope) {
$scope.customSpice = "wasabi";
$scope.spice = 'very';
$scope.spicy = function(spice) {
$scope.spice = spice;
};
}]);
SpicyController仅仅定义了一个spicy方法,这个方法需要传入spice参数。第一个按钮固定传入一个chili的参数,第二个按钮传入一个与输入框绑定的model 属性customSpice。 Scope继承示例 <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>
app.js 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';
}]);
app.css div.spicy div { padding: 10px; border: solid 2px blue; }
这里创建了三个ng-controller directives,这样就为view创建了四个scope:
Controller中方法的继承和属性一样,因此我们之前例子的属性都可以被返回字符串的方法代替。 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";
});
Controller Test: 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');
});
});
});
如果你想测试一个嵌套的controller,那你必须创建一样嵌套的scope。 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'); }); });
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |