在每个专注于 AngularJS 的教程中,我看到所有 Controller 都在一个文件 controllers.js 中。
我认为,更实际的做法是将 Controller 放在单独的文件中。
问题是:
我如何在 AngularJS 中创建分离的 Controller ? 如何将 Controller CRUD 方法放入 Controller 中(具有这样的结构)?
Controller Users
- method get
- method add
- method edit
- method delete
感谢您提供任何帮助和示例。
最佳答案
我建议这个方法(见下面的例子)
Javascript:
// first file, must be include in first in your HTML
var example = angular.module('example', []);
example.factory('IndexModel', function() {
return {
get: function() {
return 'index controller';
}
};
});
example.controller('Index', function($scope, IndexModel) {
$scope.name = IndexModel.get();
});
// other file
var example = angular.module('example'); // angular module is accessible like this
example.factory('UserModel', function() {
return {
get: function() {
return 'user controller';
}
}
});
example.controller('User', function($scope, UserModel) {
$scope.name = UserModel.get();
});
关于javascript - Angular JS 将 Controller 分离到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24032759/