[英]using ControllerAs in an angular 1.5 component
我正在嘗試在angularjs 1.5組件中使用controllerAs
語法。
這是一個小矮人https://plnkr.co/edit/mTa1bvoNi1Qew9l1xAFS?p=preview
沒有controllerAs
因為一切正常。
(function() {
angular.module("myApp", [])
.component("helloWorld", {
template: "Hello {{$ctrl.name}}, I'm {{$ctrl.myName}}!",
bindings: {
name: '@'
},
controller: helloWorldController
})
function helloWorldController() {
/* jshint validthis: true */
var vm = this;
vm.myName = 'Alain'
}
})();
但是嘗試更改為controllerAs
,我不再獲得綁定。
(function() {
angular.module("myApp", [])
.component("helloWorld", {
template: "Hello {{vm.name}}, I'm {{vm.myName}}!",
bindings: {
name: '@'
},
controller: ('helloWorldController', helloWorldController)
})
function helloWorldController() {
/* jshint validthis: true */
var vm = this;
vm.myName = 'Alain'
}
})();
您應該將controllerAs指定為屬性,如下所示:
(function() {
angular.module("myApp", [])
.component("helloWorld", {
template: "Hello {{vm.name}}, I'm {{vm.myName}}!",
bindings: {
name: '@'
},
controller: ('helloWorldController', helloWorldController),
controllerAs: 'vm'
})
function helloWorldController() {
/* jshint validthis: true */
var vm = this;
vm.myName = 'Alain'
}
})();
https://plnkr.co/edit/ThIvAnLJFhucckcRvQ3N?p=preview
有關更多信息: https : //alexpeattie.com/blog/setting-the-default-controlleras-to-vm-for-component-angular-1-5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.