2017-07-04 104 views
0

我有一個簡單的角碼不會運行,由於這個錯誤; IVE添加角路由器文件的服務器中發現它,但它不斷給我這個錯誤

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.6.5/$injector/modulerr?p0=task&p1=Error%3A%20…0Uc%20(http%3A%2F%2Flocalhost%3A3000%2Fangular%2Fangular.min.js%3A22%3A332) 
    at angular.min.js:7 
    at angular.min.js:43 
    at p (angular.min.js:8) 
    at g (angular.min.js:42) 
    at gb (angular.min.js:46) 
    at c (angular.min.js:22) 
    at Uc (angular.min.js:22) 
    at xe (angular.min.js:21) 
    at angular.min.js:333 
    at HTMLDocument.b (angular.min.js:38) 

我看來代碼

<html > 
    <head> 
     <title> 
     </title> 
    </head> 
    <body ng-app="task" ng-init="first = 1;"> 
     <h1>Task List</h1> 

     <div ng-controller="ctrl"> 
      <span>Values: {{first}}</span> 
      <input type="text" ng-model="first" /> 
     </div> 



     <script src="angular/angular.min.js"></script> 
     <script src="bower_components/angular-route/angular-route.js"></script> 
     <script src="angular/app.js"></script> 
    </body> 

</html> 

我app.js代碼

var task = angular.module('myApp', ['ngRoute']); 
task.controller('ctrl', function($scope){ 
    $scope.first = 1; 
}); 

請幫忙。

回答

2

錯誤信息中明確指出:

Failed to instantiate module task due to: Error: …0Uc (http://localhost:3000/angular/angular.min.js:22:332)

模塊名稱需要與ng-app指令中使用的名稱相匹配:

<body ng-app="task" ng-init="first = 1;"> 
//var task = angular.module('myApp', ['ngRoute']); 
var task = angular.module('task', ['ngRoute']); 
task.controller('ctrl', function($scope){ 
    $scope.first = 1; 
}); 
2

認爲你可能需要重寫略有app.js代碼:

task.controller('ctrl', ['$scope', function($scope) { 
    $scope.first = 1; 
}]); 

爲什麼初始化首先在這裏的控制器,當你還做模板,順便說一句?有沒有原因?

+0

都能跟得上只是爲了測試我的代碼工作 – Mustapha

+0

仍不起作用... – Mustapha

1

你也可以像這樣

var task = angular.module('myApp', ['ngRoute']); 
task.controller('ctrl', taskCtrl); 

function taskCtrl($scope){ 
// your logic here 
} 

而且移動你的NG-app標記分離的div和它下面把JS腳本標籤

+0

謝謝大家了優化;) – Mustapha

相關問題