【问题标题】:AngularJS - binding a function to an isolate scope via '&', not firing on ng-clickAngularJS - 通过“&”将函数绑定到隔离范围,而不是在 ng-click 上触发
【发布时间】:2016-06-26 20:29:51
【问题描述】:

我有一个带有控制器的父指令,它在 $scope 上设置一个方法。

然后,在子指令中,我尝试通过& 要求该范围方法。

但是,子指令中的ng-click 不会与父作用域函数一起触发。我做错了吗?

父指令(定义方法的地方)

app.directive('ParentDirective', [
    '$http',
    function($http) {
        return {
            restrict: 'AE',
            template: '' +
                '<div child-directive ' +
                    'list="list" ' +
                '/>' +
            '',
            controller: function ($scope, $http) {

                $scope.setSelected = function (selector) {
                    console.log('hit!', selector);
                }

                $scope.list = [];

            },
            link: function postLink(scope, element, attrs) {}
        };
    }
]);

子指令(尝试在 ng-click 上调用父方法,但不起作用)

app.directive('childDirective', [

        '$window',
        function($window) {
            return {
                restrict: 'AE',
                scope: {
                    setSelected: '&',
                    list: '=',
                },
                template: '' +
                    '<ul>' +
                        '<li ng-repeat="person in list" ng-click="setSelected(person)">{{person.uri}}</li>' +
                    '</ul>' +
                '',
                link: function postLink(scope, element, attrs) {
                }
            };
        }
    ]);

我是否将 $scope.setSelected() 错误地从父指令传递到子指令?

编辑:所以我更改了父模板以分配函数,如下所示: 模板:'' + '' + '',

this 现在将触发函数,但参数不会从子指令传递。如何让子指令将参数传递给父指令函数?

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope isolate-scope


    【解决方案1】:

    ParentDirective 有错字应该是:parentDirective

    在 parentDirective 模板中,您应该传递函数 'list="list" ' + 'set-selected="setSelected(person)"'

    试试这个:

    html:

    <parent-directive ></parent-directive>
    

    js:

    app.directive('parentDirective', [
        '$http',
        function($http) {
            return {
                restrict: 'AE',
                template: '' +
                    '<div child-directive ' +
                        'list="list" ' + 'set-selected="setSelected(person)"'+
                    '/>' +
                '',
                controller: function ($scope, $http) {
    
                    $scope.setSelected = function (selector) {
                        console.log('hit!', selector);
                    }
    
                    $scope.list = [];
    
                },
                link: function postLink(scope, element, attrs) {}
            };
        }
    ])
      app.directive('childDirective', [
    
            '$window',
            function($window) {
                return {
                    restrict: 'AE',
                    scope: {
                        setSelected: '&',
                        list: '=',
                    },
                    template: '' +
                        '<ul>' +
                            '<li ng-repeat="person in list" ng-click="setSelected({person:person})">{{person.uri}}</li>' +
                        '</ul>' +
                    '',
                    link: function postLink(scope, element, attrs) {
    
                    }
                };
            }
        ]);
    

    工作的笨蛋:

    https://plnkr.co/edit/KUWZ9bYbhnbumFwIgcIX?p=preview

    【讨论】:

    • 参数对我来说仍然是未定义的,但我知道 ng-repeat 中有数据,因为我看到了 {{person.uri}}... 但是当我尝试在 $scope.setSelected() 中注销选择器时它是未定义的.
    • 啊我在set-selected="setSelected(person) 中使用了不同的参数名称,所以它是未定义的。当我将其更改为person 时,它起作用了。谢谢!
    • 当然@Prefix很高兴它有帮助
    猜你喜欢
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多