2017-07-03 86 views
3

我宣佈這個模塊:爲什麼我在JSFiddle中找不到錯誤模塊?

angular.module('dashboard', []) 
    .controller('dashboardController', ['$scope', 
      function ($scope) { 
       $scope.data = "555"; 
      }]); 

這裏是視圖:

<div ng-app="dashboard" data-role="page" id="layersProperty" data-add-back-btn="true" > 
    <div ng-controller="dashboardController"> 
      {{data}} 
    </div> 
</div> 

這裏是FIDDLE

在控制檯中我得到這個錯誤:

Error: [$injector:nomod] Module 'dashboard' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

任何想法,爲什麼我得到上述錯誤?

+0

http://jsbin.com/qekezayipa/edit?html,js,output – Shyju

+0

確實很奇怪。您完全相同的代碼,從相同的位置加載angular.js,在plnkr正常工作:http://plnkr.co/edit/NHqBPkItZiNsqhJ8beG7?p=preview –

+3

在的jsfiddle,改變的JavaScript的負載類型是'不包 - 在'或者'不循環 - 在'而不是onLoad'的'。這是一個JSFiddle的事情,以及它如何與Angular協同工作。 – Lex

回答

2

與您的代碼沒有問題,因爲你要添加外部腳本,你只需要改變

Javascript settings -> Load type -> Wrap in <Head> enter image description here

+0

好找。這樣一個容易和奇怪的設置錯過! –

0

基本上你的代碼看起來不錯,順便說一下它的工作檢查Plnkr

檢查你的代碼(你有角?),或者只是張貼您的全部代碼。

代碼Plnkr:

<!DOCTYPE html> 
<html> 

<body> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> 

    <div ng-app="dashboard" ng-controller="dashboardController"> 
    {{ data }} 
    </div> 

    <script> 
    angular.module('dashboard', []) 
     .controller('dashboardController', ['$scope', 
     function($scope) { 
      $scope.data = "555"; 
     } 
     ]); 
    </script> 

</body> 

</html> 
1

這是由於您已經在設置的jsfiddle的JavaScript負載類型。當您使用onLoad時,JSFiddle會將您的javascript包裝在$(window).load(function(){});區塊中。這意味着註冊您的Angular應用程序的angular.module()代碼在DOM被處理之後纔會運行。所以Angular試圖找到尚未創建的dashboard模塊。

相關問題