我正在尝试将函数 (removeItem
) 传递到指令的隔离范围,然后在指令的模板中调用此函数,但是该函数被调用但没有参数。我试过这样:
<my-directive remove="removeItem(item)"></my-directive>
定义removeItem
的地方:
scope.removeItem = function(item) {
console.log('item removed');
}
隔离范围:
scope: {
remove: "&"
}
和指令模板:
<div ng-repeat="item in allItems | selectedItemFilter" >
<label>{{ item.description }}</label>
<div>
<button ng-click="remove(item)"> Remove</button>
</div>
</div>
因此,当单击删除按钮时,将调用实际的 removeItem
,但没有未定义的 item 参数。我也尝试传递简单的字符串参数,但 removeItem
从来没有得到它,它也是未定义的。什么会导致这种行为?
最佳答案
Angular 在这方面有点不直观。您实际上需要像这样指定参数名称(在 View 中):
<button ng-click="remove({item: item})">
关于javascript - 传递给隔离作用域的函数不接收参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29289400/