2016-04-11 73 views
0

我在我的HTML腳本標記中寫了一個控制器。當我運行代碼時,代碼被執行直到依賴注入,但回調函數沒有被執行。內部JS角度控制器沒有被調用

當我在其他應用程序上成功運行類似的代碼時,我無法弄清楚這有什麼問題。另外,如果我明確調用回調函數,那麼注入的依賴關係就不會被識別。

<script type="text/javascript"> 
 
    angular.module('DesignPortal.layout', []) 
 
     .controller('NgLayoutController', NgLayoutController); 
 

 
    NgLayoutController.$inject = ['$scope']; 
 

 
    function NgLayoutController($scope) { 
 
     var loggedUserId = dpConfig.userInfo.id; 
 
    } 
 
</script>

上面注入的範圍,如果函數顯式調用不識別。

回答

1

It's Working on my plunk.

<script type="text/javascript"> 
    angular.module('DesignPortal.layout', []) 
     .controller('NgLayoutController', NgLayoutController); 

    NgLayoutController.$inject = ['$scope']; 

    function NgLayoutController($scope) { 
     alert("Its working!"); 
     var loggedUserId = dpConfig.userInfo.id; 
    } 
</script> 

This happens due to a number of reasons such as removing a script for index.html for a module but leaving in the module dependency or even misspelling a module dependency.

你需要做的第一件事是確保所有模塊的依賴關係的拼寫正確,然後確保所有文件都加載。

+1

是的,它的確如此。我沒有做的事情是我沒有用ng-controller指令在HTML中包含控制器。我理解,包含在HTML中並不是必需的。但是,顯然我錯了。 –

0

試試這個。我沒有嘗試過自己,但它應該工作。

<script type="text/javascript"> 
    angular.module('DesignPortal.layout', []) 
     .controller('NgLayoutController',['$scope', function($scope) { 
      function NgLayoutController() { 
       var loggedUserId = dpConfig.userInfo.id; 
      } 
     }]);  
</script> 
+0

已經做了,沒有爲我工作。 –

1

控制檯中是否有錯誤? 這可能是您在加載角度之前加載腳本。 試着將你的代碼封裝在函數中,並放置一些斷點/ debugs/console.log來測試你的代碼在哪一點執行。

(function(){ 
    "use strict"; 

    angular.module('DesignPortal.layout', []) 
     .controller('NgLayoutController', NgLayoutController); 

    NgLayoutController.$inject = ['$scope']; 

    function NgLayoutController($scope) { 
     var loggedUserId = dpConfig.userInfo.id; 
    } 
})(); 
+0

我已經照顧過這樣的事情,並且沒有控制檯中的錯誤。 –

相關問題