检测在控制器中调用方法的指令

时间:2014-03-11 18:34:18

标签: angularjs angularjs-directive angularjs-scope angularjs-controller

有没有办法检测哪个指令在控制器中调用一个函数而不从指令本身传递任何参数。

演示我需要的简单示例:

控制器:

 app.controller('mainCtrl', function ($scope) {
            // detect the directive calling the displayDirective function.
            var detectedDirective = ""; // <--
            $scope.displayDirective = function (message) {
                alert(message + detectedDirective );
            }
        }

    )

指令:

app.directive('directive1', function () {
        return {
            restrict: 'A',
            scope: {
                update: "&"
            },
            link: function (scope, ele, attrs) {
                scope.update({message: "I am"});
            }
        }})

app.directive('directive2', function () {
        return {
            restrict: 'A',
            scope: {
                update: "&"
            },
            link: function (scope, ele, attrs) {
                scope.update({message: "I am"});
            }
       }})

for plnkr

0 个答案:

没有答案