简体   繁体   English

子组件未调用AngularJS ES6父函数

[英]AngularJS ES6 parent function not being called by child component

I'm a bit new to the ES6 syntax/structure of Angular 1.x, and I'm running into an issue with passing a function from a parent controller to a child controller. 我对Angular 1.x的ES6语法/结构有些陌生,并且在将函数从父控制器传递到子控制器时遇到了问题。

This is how the app is tied together (I use webpack + babel with this as the entry-point): 这是应用程序捆绑在一起的方式(我将webpack + babel用作入口点):

const requires = [
    'ngRoute',
];

//App
angular.module('kanban',requires)
    .controller('dashboardCtrl', dashboardCtrl)
    .component('board', board)
    .component('task', task)
    .config(routes);

In my routes, I have a single route, which is my 'parent' 在我的路线中,我只有一条路线,这就是我的“父母”

export default function($routeProvider) {
    $routeProvider
        .when('/', {
            template: dashboardTemplate,
            controller: 'dashboardCtrl',
            controllerAs: '$ctrl',
        });
}

Who's controller looks like this: 谁的控制者如下所示:

export default function($rootScope) {
    $rootScope.title = 'Kanban';
    let _this = this;

    this.boards = [
        {
            _id: 'b1',
            title: 'backlog',
            tasks: ['t1', 't2'],
        }
    ];

    this.deleteBoard = function(board) {
        console.log(board);
        let index = _this.boards.indexOf(board);
        if (index !== -1) {
            _this.boards.splice(index, 1);
        }
    };

And in the template, the child is created with ng-repeat, passing in the function 然后在模板中,使用ng-repeat创建子级,并传入函数

<board ng-repeat="board in $ctrl.boards" board="board" onDelete="$ctrl.deleteBoard(board)" ></board>

And the board binds the attribute as a function with an & 董事会将属性绑定为带有&的函数

export const board = {
    template: boardTemplate,
    controller: boardCtrl,
    bindings: {
        board: '=',
        onDelete: '&',
    }
};

And the function is added to the controller within a different function: 并将该功能添加到不同功能中的控制器中:

export default function boardCtrl() {
    let _this = this;

    this.deleteBoard = function(){
        console.log(_this.onDelete);
        _this.onDelete({board: _this.board});
    };
}

And called with a click: 并点击一下:

<button ng-click="$ctrl.deleteBoard()"></button>

I can reach the board (child) controller's function, which prints this in the console: 我可以访问板(子)控制器的功能,该功能将在控制台中显示:

function (locals) {
    return parentGet(scope, locals);
}

And returns no errors, but the console.log in the parent deleteBoard function does not get called. 并且不返回任何错误,但是父deleteBoard函数中的console.log不会被调用。

What is happening here? 这是怎么回事 Why does the child seem to recognize that it is calling something in the parent scope, but is not reaching it? 为什么孩子看起来似乎认识到它在父级作用域中正在调用某些东西,但是没有达到?

Turns out this issue was because of how the attribute was named in the parent template, where 事实证明,此问题是由于在父模板中如何命名属性,其中

<board onDelete="$ctrl.deleteBoard(board)"></board>

needed to be the following instead: 需要改为以下内容:

<board on-delete="$ctrl.deleteBoard(board)"></board>

even though the attribute is bound as "onDelete" on the child controller. 即使该属性在子控制器上绑定为“ onDelete”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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