Angularjs 指令控制器中的访问要求控制器

Angularjs 指令控制器中的访问要求控制器,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我不想使用link方法,而是controller方法。 是否有办法在指令addProduct的控制器方法中获取mainCtrl 比如: app.directive('mainCtrl', function () { return { controller: function () { this.funcA = function(){} } }; }); app.directive('addProduct', functio

我不想使用link方法,而是controller方法。 是否有办法在指令addProduct的控制器方法中获取mainCtrl

比如:

app.directive('mainCtrl', function () {
    return {
        controller: function () {
            this.funcA = function(){}
        }
    };
});

app.directive('addProduct', function () {
    return {
        restrict: 'E',
        require: '^mainCtrl',
        link: function (scope, lElement, attrs, mainCtrl) {
            mainCtrl.funcA()
        }
    };
});

您仍然需要使用
链接
功能,因为控制器被注入其中。但是,您可以请求指令自己的控制器,然后将另一个必需的控制器设置为其属性:

app.directive('addProduct', function () {
    return {
        restrict: 'E',
        require: '^mainCtrl',
        controller: function (scope, mainCtrl) {
            mainCtrl.funcA()
        }
    };
});
以下是我的解决方案:

app.directive('addProduct', function () {
    return {
        restrict: 'E',
        require: ['addProduct','^mainCtrl'],
        controller: function ($scope) {
            // this.mainCtrl is still not set here
            // this.mainCtrl.funcA(); // this will cause an error

            // but typically it is invoked in response to some event or function call
            $scope.doFuncA = function(){
               this.mainCtrl.funcA();
            }
        },
        link: function(scope, element, attrs, ctrls){
          var me = ctrls[0], mainCtrl = ctrls[1];
          me.mainCtrl = mainCtrl;
        }
    };
});

自AngularJS 1.5以来,您可以使用控制器的
$onInit
生命周期挂钩。如中所述,在将
require
定义为对象并将
bindToController
设置为
true
时,所需的控制器将在构造控制器之后,但在运行
$onInit
方法之前,作为属性添加到控制器中。因此,代码如下所示:

app.directive('mainCtrl',function(){
返回{
控制器:函数(){
this.funcA=函数(){}
}
};
});
应用指令('addProduct',函数(){
返回{
限制:'E',
要求:{
myParentController:“^mainCtrl”
},
bindToController:对,
控制器:功能($scope){
这是。$onInit=function(){
this.myParentController.funcA();
};
}
};
});

通过链接功能将控制器传递到作用域,然后访问控制器上的作用域。像这样:

app.directive('mainCtrl', function () {
    return {
        controllerAs: 'main',
        controller: function () {
            this.funcA = function(){}
        }
    };
});

app.directive('addProduct', function () {
    return {
        restrict: 'E',
        require: '^mainCtrl',
        controller: function ($scope) {
            $scope.main.funcA();
        }
    };
});

您可以制作一个独立的控制器,并将其添加到两个方向。下面的答案回答了您的问题?如果我想使用ngModelControllerIt,该怎么办?ngModelControllerIt不适用于角度方向,但它是定制方向的一个很好的解决方案,或者您可以将其添加到范围中,而不需要自己的控制器。
app.directive('mainCtrl', function () {
       return {
            controller: function () {
                this.funcA = function(){}
            }
        };
    });

app.directive('addProduct', function () {
    return {
        restrict: 'E',
        require: '^mainCtrl',
        link: function (scope, lElement, attrs, mainCtrl) {
            scope.ctrl=mainCtrl;
        },controller:function($scope){
            $scope.ctrl.funcA();
        }
    };
});