“Uncaught Error: [$injector:unpr]” with angular after deployment

后端 未结 7 1202
灰色年华
灰色年华 2020-11-30 21:45

I have a fairly simple Angular application that runs just fine on my dev machine, but is failing with this error message (in the browser console) after I deploy it:

7条回答
  •  独厮守ぢ
    2020-11-30 22:14

    This problem occurs when the controller or directive are not specified as a array of dependencies and function. For example

    angular.module("appName").directive('directiveName', function () {
        return {
            restrict: 'AE',
            templateUrl: 'calender.html',
            controller: function ($scope) {
                $scope.selectThisOption = function () {
                    // some code
                };
            }
        };
    });
    

    When minified The '$scope' passed to the controller function is replaced by a single letter variable name . This will render angular clueless of the dependency . To avoid this pass the dependency name along with the function as a array.

    angular.module("appName").directive('directiveName', function () {
        return {
            restrict: 'AE',
            templateUrl: 'calender.html'
            controller: ['$scope', function ($scope) {
                $scope.selectThisOption = function () {
                    // some code
                };
            }]
        };
    });
    

提交回复
热议问题