简体   繁体   English

将参数传递给指令的父控制器功能

[英]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.

相关问题 从指令调用父控制器的函数 - Call Parent Controller's Function from Directive 将值从父指令传递给子指令的模板函数 - passing value from parent directive to child directive's template function Angular - 在child指令的控制器中获取父指令的控制器(不是链接函数) - Angular - get parent directive's controller in child directive's controller (not link function) 将JSON对象作为参数传递给指令的链接函数 - Passing JSON Objects as Parameters to a Directive's Link Function 角指令将参数传递给元素的.html()中的控制器函数 - Angular directive passing arguments to controller function within element's .html() 将父指令的控制器传递给指令控制器(就像在“链接”函数中完成的那样) - Pass parent directive's controller to a directive controller (like it is done in the "link" function) AngularJS-将参数从指令传递回控制器 - AngularJS - Passing parameters from directive back to the controller 将数据从指令传递到父范围控制器 - Passing data from directive to parent scope controller 将父控制器方法传递给包含子指令的对象 - Passing parent controller method to transcluding child directive 将参数从控制器传递到模式指令 - Passing parameters from a controller to a modal directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM