Javascript 使用不带“的模板”;controllerAs“;前缀
如果您看到代码,您会注意到我将Javascript 使用不带“的模板”;controllerAs“;前缀,javascript,angularjs,Javascript,Angularjs,如果您看到代码,您会注意到我将template属性设置为{{$.aaa}。有没有办法避免使用变量前的前缀controllerAs并使用{{aaa}}格式的模板 (function(){ "use strict"; class Controller { constructor(){ this.aaa = 111; } doSomething(){ alert(this.aaa)
template
属性设置为{{$.aaa}
。有没有办法避免使用变量前的前缀controllerAs
并使用{{aaa}}
格式的模板
(function(){
"use strict";
class Controller {
constructor(){
this.aaa = 111;
}
doSomething(){
alert(this.aaa)
}
}
var app = angular.module('app', []);
app.directive('any', function() {
return {
restrict: 'E',
controller: Controller,
controllerAs: '$',
template: '{{$.aaa}}'
};
});
})();
将$scope注入控制器构造函数,然后将属性附加到scope
class Controller {
static $inject = ["$scope"];
constructor($scope) {
$scope.aaa = "value";
}
}
以及:
在这种情况下,“aaa”应该是$scope的属性,而不是控制器的属性。请注意,controllerAs语法是当今首选的方式。
app.directive('any', function() {
return {
restrict: 'E',
controller: Controller,
template: '{{aaa}}'
};
});