AngularJS : minification issue in directive

前端 未结 2 541
广开言路
广开言路 2020-12-28 13:23

I have yet another issue with minification. This time it\'s because of the $scope service passed to the directive\'s controller. See below code:

angular.modu         


        
相关标签:
2条回答
  • 2020-12-28 14:00

    ok, I ended up creating the controller in a separate file :

    angular.module('person.controllers').controller('personCtrl', ['$scope', function ($scope) {
    $scope.test = 3;
    }]);
    

    then in the directive, I assign the controller by name:

    controller: 'personCtrl'
    

    Not sure it's the best way. It looks clean though. What do you think ?

    0 讨论(0)
  • 2020-12-28 14:08

    You need to declare a controller as follows:

    controller: ['$scope', function ($scope)
        {                   
            $scope.test = 3;                   
        }]
    

    Full example here:

    angular.module('person.directives').
    directive("person", ['$dialog', function($dialog) {
    return {
        restrict: "E",
        templateUrl: "person/views/person.html",
        replace: true,
        scope: {
            myPerson: '='
        },     
        controller: ['$scope', function ($scope)
        {                   
            $scope.test = 3;                   
        }]
    }
    }]);
    

    A solution provided by @Sam would work to but it would mean exposing directive's controller to the whole application which is unnecessary.

    0 讨论(0)
提交回复
热议问题