3 回答

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊
文件一:
angular.module('myApp.controllers', []);
文件二:
angular.module('myApp.controllers').controller('Ctrl1', ['$scope', '$http', function($scope, $http){}]);
檔案三:
angular.module('myApp.controllers').controller('Ctrl2', ['$scope', '$http', function($scope, $http){}]);
包括在那個(gè)順序中。我推薦3個(gè)文件,因此模塊聲明是獨(dú)立的。
關(guān)于文件夾結(jié)構(gòu),關(guān)于這個(gè)主題有很多很多意見(jiàn),但這兩個(gè)都很不錯(cuò)
https://github.com/angular/angular-seed
http://briantford.com/blog/huuuuuge-angular-apps.html

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超2個(gè)贊
在末尾使用帶有數(shù)組的angular.module API 將告訴angular創(chuàng)建一個(gè)新模塊:
myApp.js
// It is like saying "create a new module"angular.module('myApp.controllers', []); // Notice the empty array at the end here
在沒(méi)有數(shù)組的情況下使用它實(shí)際上是一個(gè)getter函數(shù)。因此,要分離您的控制器,您可以:
Ctrl1.js
// It is just like saying "get this module and create a controller"angular.module('myApp.controllers').controller('Ctrlr1', ['$scope', '$http', function($scope, $http) {}]);
Ctrl2.js
angular.module('myApp.controllers').controller('Ctrlr2', ['$scope', '$http', function($scope, $http) {}]);
在你的javascript導(dǎo)入過(guò)程中,只需確保myApp.js在AngularJS之后,但在任何控制器/服務(wù)/等之前...否則angular將無(wú)法初始化你的控制器。

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
雖然這兩個(gè)答案在技術(shù)上都是正確的,但我想為此答案引入不同的語(yǔ)法選擇。這個(gè)imho可以更容易地閱讀注射的內(nèi)容,區(qū)分等。
文件一
// Create the module that deals with controllers
angular.module('myApp.controllers', []);
文件二
// Here we get the module we created in file one
angular.module('myApp.controllers')
// We are adding a function called Ctrl1
// to the module we got in the line above
.controller('Ctrl1', Ctrl1);
// Inject my dependencies
Ctrl1.$inject = ['$scope', '$http'];
// Now create our controller function with all necessary logic
function Ctrl1($scope, $http) {
// Logic here
}
文件三
// Here we get the module we created in file one
angular.module('myApp.controllers')
// We are adding a function called Ctrl2
// to the module we got in the line above
.controller('Ctrl2', Ctrl2);
// Inject my dependencies
Ctrl2.$inject = ['$scope', '$http'];
// Now create our controller function with all necessary logic
function Ctrl2($scope, $http) {
// Logic here
}
添加回答
舉報(bào)