2015-12-21 88 views
0

我想分割我的角碼,我有一些問題。分割多個文件中的角碼

在beggining我有一個「未定義功能」我的控制器「現在我有‘錯誤:[$注射器:NOMOD] http://errors.angularjs.org/1.4.8/ $噴油器/ NOMOD P0 = myApp.controllers.accueil’和」錯誤: $ injector:modulerr] http://errors.angularjs.org/1.4.8/ $ injector/modulerr?p0 = myApp & p1 =%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.8%2F%24injector%2Fmodulerr%3Fp0% 3DmyApp.controllers.accueil%26p1%3D%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.4.8%252F%2524injector%...「errors。 我發現了其他一些問題看起來相似,但我找不到我的錯誤(s)...

Here there is my folders

這是我的index.html:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8" /> 
     <meta name="author" content="M2-SCMN"> 
    <link rel="icon" href="img/favicon.ico" /> 
    <link rel="stylesheet" type="text/css" href="web/css/bootstrap.min.css"> 
    <script type="text/javascript" src="web/js/angular.min.js"></script> 
    <script type="text/javascript" src="web/js/angular-route.min.js"></script> 
    <script type="text/javascript" src="myApp.js"></script> 
    <script type="text/javascript" src="controllers/accueil.js"></script> 
    <script type="text/javascript" src="controllers/utilisateur.js"></script> 
     <title>AngularProj</title> 
    </head> 
    <body ng-app="myApp" > 
    <div ng-view></div> 
    </body> 
</html> 

我app.js(我想有1文件/模塊(用戶,網頁...)):

angular.module('myApp', [ 
    'ngRoute', 
    'myApp.controllers.accueil', 
    'myApp.controllers.utilisateur' 
]) 
.config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/', {templateUrl: 'views/accueil.html', controller: 'AccueilCtrl'}); 
    $routeProvider.when('/connexion', {templateUrl: 'views/connexion.html', controller: 'ConnexionCtrl'}); 
    $routeProvider.otherwise({redirectTo: '/'}); 
}]); 

而且ACCUEIL控制器的樣子:

angular.module('myApp.controllers.accueil') 
    .controller('AccueilCtrl', function ($scope) { 
     $scope.texte = 'Hello, Angular fanatic.'; 
    }); 

回答

0

你收到這個錯誤,因爲你沒有實際創建您的myApp.controllers.accueil模塊在accueil控制器文件中,您只是試圖檢索模塊。

您首先需要創建myApp.controllers.accueil模塊。我認爲你的困惑來自於你如何創建一個模塊和你如何找回模塊之間的差異。

AngularJS docs兩者(創建下見與檢索):

Beware that using angular.module('myModule', []) will create the module myModule and overwrite any existing module named myModule. Use angular.module('myModule') to retrieve an existing module.

所以你實際上是試圖檢索不存在的模塊。

控制器代碼應該是這樣的,而不是:

angular.module('myApp.controllers.accueil', []) 
.controller('AccueilCtrl', function ($scope) { 
    $scope.texte = 'Hello, Angular fanatic.'; 
});