簡體   English   中英

每當控制器在指令中使用隱式注釋時,Angular Strict Di都不會引發錯誤

[英]Angular Strict Di does not throw error whenever a controller uses implicit annotation within Directive

我在縮小的angularJS 1.x應用程序中遇到以下問題

Uncaught Error: [$injector:unpr] Unknown provider: nProvider <- n

根據其他相關問題的建議,我在引導我們的應用程序時添加了嚴格的依賴注入,如下所示

angular.bootstrap(document, [app.name], { strictDi: true });

即使在我添加了此代碼之后,當我的應用程序使用非最小化代碼運行時,我也沒有遇到任何問題,但是當我縮小代碼時,我面臨着未知的提供程序錯誤。 根據angularjs中的文檔,如果在使用嚴格的依賴注入時有任何隱式注釋,則會拋出錯誤。 但是不知何故它沒有這樣做。

我終於發現問題出現在其中一個具有$ scope隱式注釋的控制器的指令中,如下所示

angular.module('MyModule', [])
.directive('myTabs', function() {
  return {
    restrict: 'E',
    transclude: true,
    scope: {},
    controller: function MyTabsController($scope) {
      var panes = $scope.panes = [];

      $scope.select = function(pane) {
        angular.forEach(panes, function(pane) {
          pane.selected = false;
        });
        pane.selected = true;
      };

      this.addPane = function(pane) {
        if (panes.length === 0) {
          $scope.select(pane);
        }
        panes.push(pane);
      };
    },
    templateUrl: 'my-tabs.html'
  };
})

使用內聯數組注釋后,該問題得到解決。

我只是想知道為什么嚴格di不會拋出任何錯誤? 我應該在代碼中進行其他更改嗎?”

我使用手動引導程序和自動引導程序方法創建了帶有嚴格依賴項注入的簡單角度應用程序。

柱塞鏈接如下:

手動引導程序: https : //plnkr.co/edit/9QoKpzzjvFHMT1ZdTIoy? p =preview

自動引導程序: https//plnkr.co/edit/kBXOpY? p = preview

我發現在這兩種情況下,如果控制器都具有依賴項的隱式批注,則引導程序上的角度拋出錯誤如下:

angular.js:12798 Error: [$injector:strictdi] ScrollDirectiveController is not using explicit annotation and cannot be invoked in strict mode
http://errors.angularjs.org/1.4.12/$injector/strictdi?p0=ScrollDirectiveController
    at angular.js:68
    at Function.annotate [as $$annotate] (angular.js:3825)
    at Object.invoke (angular.js:4548)
    at extend.instance (angular.js:9435)
    at nodeLinkFn (angular.js:8540)
    at compositeLinkFn (angular.js:7975)
    at nodeLinkFn (angular.js:8571)
    at compositeLinkFn (angular.js:7975)
    at compositeLinkFn (angular.js:7978)
    at publicLinkFn (angular.js:7855)

我們應用程序中的問題是,控制器定義使用內聯數組注釋,而沒有指定任何依賴項,如下面的代碼所示:

function scrollDirective($window) {
  return {
    restrict: 'A',
    scope: {
      callback: '&scrollDirective'
    },
    link: function(scope, element, attrs) {
         //some code here
    },
    controller: [function ScrollDirectiveController($scope) {
      console.log("controller loaded!!");
    }]
  };

我還為同一對象創建了一個插件: https ://plnkr.co/edit/nkzvmeoeV9ARHVMzWHFI?p = preview

在這種情況下,角度不會引發任何錯誤

暫無
暫無

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

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