javascript - AngularJS 指令 - 将函数作为参数传递给 "&"属性

标签 javascript angularjs function angularjs-directive

我对 angular 指令 非常了解,我正在尝试做一些非常简单的事情,但我认为它在 angular 中不受支持。
我正在尝试将函数作为参数传递给指令隔离范围(类型 &)。
我只想将“@”范围值传递给在我的 Controller 中实现的函数。
似乎不可能用参数调用“&”范围属性。

<div ng-app="MyApp">
    <div ng-controller="MyController">
        <testkeyvalue accept="blabla" key='keyA' value='valueA' />
    </div>
</div>

var myApp = angular.module('MyApp', [])
myApp.directive('testkeyvalue', function ()
{
    return {
        restrict: 'E',
        replace: true,
        scope: {
            key: '@',
            value: '@',
            accept: "&"
        },
        template: '<div><label class="control-label">{{key}}</label><br/>' +
        '<label class="control-label">{{value}}</label>' ,



        link: function (scope, element, attrs)
        {

            var arr = ["key", "value", "accept"];
            for (var i = 0, cnt = arr.length; i < arr.length; i++)
            {

                scope.$watch(arr[i], function ()
                {
                    cnt--;
                    if (cnt <= 0)
                    {
                        fireaccept()
                    }
                });
            }

            var fireaccept = function ()
            {
                //This is the problem
                //I can't deliver this arguments to the "blabla" function im my controller
                scope.accept(scope.key, scope.value);
            }
        }
    };
});


myApp.controller('MyController', function ($scope, $window)
{
    $scope.blabla = function (key, val)
    {
        $window.alert("key:" + key + " val: " + val);
    };
});

这是完整的演示: http://jsfiddle.net/chezih/y85Ft/401/

我读过这个问题:AngularJS: How to pass arguments/functions to a directive?
但这不是我要找的,我只想注册 Controller ,从指令内部触发的事件(并且事件可以传递参数)。

有什么办法可以做到这一点?

最佳答案

这是这样工作的:

http://jsfiddle.net/Pascalz/hr8zua7q/

<div ng-app="MyApp">
    <div ng-controller="MyController">
        <testkeyvalue accept="blabla(key, val)" key='keyA' value='valueA' />
    </div>
</div>

var myApp = angular.module('MyApp', [])
myApp.directive('testkeyvalue', function ()
{
    return {
        restrict: 'E',
        replace: true,
        scope: {
            key: '@',
            value: '@',
            accept: "&"
        },
        template: '<div><label class="control-label">{{key}}</label><br/>' +
        '<label class="control-label">{{value}}</label>' ,

        link: function (scope, element, attrs)
        {
            var arr = ["key", "value", "accept"];
            for (var i = 0, cnt = arr.length; i < arr.length; i++)
            {      
                scope.$watch(arr[i], function ()
                {
                    cnt--;
                    if (cnt <= 0)
                    {
                        fireaccept()
                    }
                });
            }

            var fireaccept = function ()
            {
                //This is the problem
                //I can't deliver this arguments to the "blabla" function im my controller
                scope.accept({key:scope.key, val:scope.value});
            }
        }
    };
});


myApp.controller('MyController', function ($scope, $window)
{
    $scope.blabla = function (key, val)
    {
        $window.alert("key:" + key + " val: " + val);
    };
});

关于javascript - AngularJS 指令 - 将函数作为参数传递给 "&"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25484543/

相关文章:

html - Angular 2 选择 <li> 并获取文本

javascript - Angular select 和 ng-model

javascript - 粘贴键盘快捷键后调用JS函数?

javascript - 主干点击事件在 Firefox 中无法正常工作

javascript - React.js - 兄弟组件之间的通信

angularjs - 如何在2个 Angular 应用程序之间使用iframe进行通信?

php - 调用未定义函数 testinput() 第 48 行

javascript - 将从 javascript 函数返回的值分配给变量

php - 通过 jQuery 检查属性 href

javascript - 合并并添加数组中的记录