简体   繁体   中英

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:

<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 . However upon checking, {{todo._id}} interpolates correctly, so the value does exist. 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="deleteTodo(todo._id)"

To this:

ng-click="deleteTodo({id: todo._id})"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM