简体   繁体   English

自定义指令的角度控制器

[英]Angular Controller on Custom Directive

Here is my controllers.js file 这是我的controllers.js文件

(function(ctx,angular){
    'use strict';
    angular.module('app.controllers')

    .controller('SearchMasterController',['$scope',function($scope){
        //My Code
    }]);

})(window, angular);

And this is my directives.js file 这是我的directives.js文件

(function(ctx,angular){
    function ControllerFunction(){
        //My Controller Code
    }
    var directiveConfig = {
        restrict:'E',
        templateUrl:'path/to/acco.html',
        controller: ControllerFunction
    }

    angular.module('app.directives')
    .directive('acco', function(){
        return directiveConfig;
    });
})(window, angular);

Now my question is, can I use this acco directive with some different controller. 现在我的问题是,我可以将此acco指令与其他控制器一起使用吗? Ideally, is there any way to get it working like 理想情况下,有什么方法可以像

<acco ng-controller="SearchMasterController"></acco> ? <acco ng-controller="SearchMasterController"></acco>吗?

I tried doing, 我试着做

<acco>
    <div ng-controller="SearchMasterController"></div>
</acco>

and it seems to work. 而且似乎可行。

Is it possible to use 是否可以使用

<acco ng-controller="SearchMasterController"></acco> ? <acco ng-controller="SearchMasterController"></acco>吗?

The latter alternative looks ugly to me. 后一种选择对我来说很难看。

nice to heard this type of access, i have tried 很高兴听到这种访问方式,我已经尝试过

<acco>hi{{name1}}
    <div ng-controller="SearchMasterController">{{name1}}</div>
</acco>
<acco ng-controller="SearchMasterController">{{name1}}</acco>
<script>
            angular.module('myApp', [])
                    .controller('SearchMasterController', ['$scope', function ($scope) {
                            //My Code
                            console.log("search");
                            $scope.name1 = 'james';
                        }])
                    .directive('acco', function () {
                        return{
                            restrict: 'E',
                            templateUrl: 'acco.html',
                            controller: function($scope) {
                                //My Controller Code
                                console.log("cntrlr fn");
                                $scope.name1 = 'semaj';
                            }
                        };
                    });
</script>

@that time i getting output as @那个时候我得到输出

cntrlr fn
search
cntrlr fn           

means if we are using like 意味着如果我们使用像

<acco>hi{{name1}}
    <div ng-controller="SearchMasterController">{{name1}}</div>
</acco>

then we can access both controllers but when we are using like 然后我们可以访问两个控制器,但是当我们使用like

<acco ng-controller="SearchMasterController">{{name1}}</acco>

we can't access SearchMasterController and it's not loaded also.. 我们无法访问SearchMasterController,它也未加载。

Yes you can use some different controller for your directive, but there is some best practice 是的,您可以为指令使用其他控制器,但是有一些最佳实践

Use controller when you want to expose an API to other directives. 当您要将API暴露给其他指令时,请使用控制器。 Otherwise use link. 否则使用链接。

The way you tried to use controller doesn't make much sense 您尝试使用控制器的方式没有多大意义

<!--here acco and ng-controller both are directives,
    in your directive's 'ControllerFunction' and ng-controller's 'SearchMasterController'
    has the same controll (scope) for 'acco' rendered html.
    In that case your directive's controller overrite ng-controller functionality.
    So leave 'ng-controller',
    if you need any functionality in your directive
    then pass those functionality using =,&,@-->

<acco ng-controller="SearchMasterController"></acco>

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

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