簡體   English   中英

使用指令控制器進行角度縮小?

[英]Angular minification with directive controller?

如果我有以下內容:

myapp.directive('directivename', ...

    return {
        ...
        restrict: 'E',
        controller: MyController,
        ...
    }

    function MyController($scope, $somethingelse) {
        // Contents of controller here
    }
);

我如何修改它,以便MyController在縮小時不會被破壞? 我收到以下錯誤:

錯誤:[$ injector:unpr]未知提供者:eProvider < - e

可以使用顯式依賴項注釋來解決它。 你有什么隱含的注釋,在縮小時會導致問題。 您也可以使用$inject或inline數組注釋來注釋指令中的依賴項。

MyController.$inject = ['$scope', '$somethingelse'];

function MyController($scope, $somethingelse) {
    // Contents of controller here
}

或者在指令中:

return {
    ...
    restrict: 'E',
    controller: ['$scope', '$somethingelse', MyController],
    ...
}

或者使用.controller語法注冊您的控制器

app.controller('MyController', ['$scope', '$somethingelse', MyController]);

並在指令中設置控制器名稱而不是構造函數。

return {
    ...
    restrict: 'E',
    controller: 'MyController',
    ...
}

您還可以查看ng-annotate ,您不需要使用顯式注釋。

通常,使用以下方法:

myapp.controller('MyController', ['$scope', '$somethingelse', function($scope, $somethingelse) {
  ...
}]);

避免這樣的問題。


你可以像這樣使用:

return {
    restrict: 'EA',
    template: ...,
    scope: {},
    controller: ["$scope","$rootScope", function ($scope,$rootScope) {
       //code here
    }],
    link: function (scope) {
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM