controllerAs的使用
1.controllerAs在路由配置中的使用
.state('account.register', {
url: '/register',
templateUrl: 'app/account/register.html',
controller: 'RegisterCtrl',
controllrtAs: 'vm'
})
通过controllerAs把vm对象挂载到$scope对象下
$scope -> vm
2.controllerAs在自定义指令中的使用
1. 设置scope:{},bindToController属性绑定到controller
'use strict';
angular.module('nodeInAction')
.directive('countCard', function () {
return {
restrict: 'E',
controllerAs: 'vm',
scope: {},
bindToController: {
icon: '@',
title: '@',
count: '@',
unit: '@',
colorStyle: '@'
},
templateUrl: 'app/components/countCard/countCard.html',
controller: function () {
var vm = this;
console.log(vm);
}
};
});
---
2. 正常设置scope对象的参数,bindToController参数值改为true
'use strict';
angular.module('nodeInAction')
.directive('countCard', function () {
return {
restrict: 'E',
scope: {
icon: '@',
title: '@',
count: '@',
unit: '@',
colorStyle: '@'
},
controllerAs: 'vm',
bindToController: true,
templateUrl: 'app/components/countCard/countCard.html',
controller: function () {
var vm = this;
console.log(vm);
}
};
});