我试图使用类来定义指令。html确实会出现,并显示它正在调用ctor和控制器函数。但是,我有一个类方法,我想通过ng单击在html中调用它,但是它没有被调用。
注意:我在1.3xan中工作,现在无法升级。
https://plnkr.co/edit/cWyyCahfoWWwk0UN49ep?p=preview
指令:
class StoreHours{
constructor(){
console.log("inside ctor");
this.restrict = 'E';
this.templateUrl = 'storeHours.html';
this.bindToController = true;
this.controllerAs = 'vm';
this.scope = {
hours: '=',
index: '=',
addNewHour: '&'
};
}
controller(){
console.log("inside controller");
}
// the member metod that I want ng-click to call
addNew(newHour){
var vm = this;
console.log("addNew()");
vm.addNewHour({ hour: newHour, index: vm.index });
vm.newHour = "";
}
// don't need this but just showing
link(scope, element, attrs){
}
}
app.directive('storeHours', () => new StoreHours);
HTML:
<div style="background-color: gray;">
<h1>I'm the store hours component!</h1>
<li ng-repeat="hrs in vm.hours">
{{hrs}}
</li>
<input type="text" ng-model="vm.newHour" />
<button ng-click="vm.addNew(vm.newHour)">Add New</button>
</div>
发布于 2018-04-17 17:36:40
addNew()
方法附加到类作用域,无论是否需要将它附加到controller()
作用域(这是不同的)。
在控制器中添加该方法,您应该会没事的。
controller(){
console.log("inside controller");
var vm = this;
vm.addNew = function(newHour){
console.log("addNew()");
vm.addNewHour({ hour: newHour, index: vm.index });
vm.newHour = "";
}
}
https://stackoverflow.com/questions/49883780
复制相似问题