[英]Passing parameters to directive's parent controller function
I've read a few questions on this and I still can't figure out how to solve this problem. 我已经阅读了一些有关此问题,但仍然不知道如何解决此问题。 Simply, I have a custom directive: 简单来说,我有一个自定义指令:
.directive('todoList', function() {
return {
restrict: 'AE',
scope: {
todos: '=todos',
deleteTodo: '&'
},
replace: true,
templateUrl: '/public/partials/todoList.html'
}
})
My custom directive looks like this: 我的自定义指令如下所示:
<todo-list todos="todo.todos" delete-todo="todo.deleteTodo(id)"></todo-list>
Inside my controller it looks like this: 在我的控制器中,它看起来像这样:
function deleteTodo(id) {
console.log(id) // undefined
todoService.deleteTodo(id)
.then(function(todo) {
todoService.getTodos();
}, function(err, status) {
todoService.getTodos();
})
}
// using controllerAs
this.deleteTodo = deleteTodo;
My HTML for my directive is like this: 我的指令HTML格式如下:
<div ng-repeat="todo in todos track by $index">
<span class="item">{{todo.name}}<button type="button" class="btn btn-danger delete" ng-click="deleteTodo(todo._id)">Delete Item</button></span>
</div>
The problem is that todo._id
as a parameter comes up as undefined
when passed to the controller function in my console.log
. 问题在于,当将todo._id
作为参数传递给console.log
的控制器函数时,它的undefined
。 However upon checking, {{todo._id}}
interpolates correctly, so the value does exist. 但是,检查后, {{todo._id}}
正确插值,因此该值确实存在。 I know I am missing something, but I can't figure out what. 我知道我缺少什么,但我不知道是什么。 Could someone please help me out? 有人可以帮我吗?
My apologies, I just solved it. 抱歉,我刚刚解决了问题。
Inside my ng-click, I changed it from this: 在ng-click内,我对此进行了更改:
ng-click="deleteTodo(todo._id)"
To this: 对此:
ng-click="deleteTodo({id: todo._id})"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.