[英]Best practice of angular code structuring
问题是我不知道在Angular.JS中将每个单独的代码实体(控制器,模型,服务等)放置在单独的.js文件中是否可行。 我目前正在尝试以这种方式实施我的解决方案,但是感觉并不对。
例:
step.js内容(模型原型):
(function() {
var moduleStep = angular.module('step', []);
moduleStep.config(function() {
var defaults = {
title: "",
enabled: true,
active: false,
visited: false,
viewUrl: "/clientTemplates/notification/step1.html",
model: {}
};
/**
* @param {string} title
* @param {string} viewUrl
* @param {object} model [optional]
* @constructor
*/
moduleStep.Step = function(title, viewUrl, model) {
_.extend(this, defaults);
this.title = title;
this.viewUrl = viewUrl;
_.isUndefined(model) && (this.model = model);
};
var prot = moduleStep.Step.prototype;
/**
* @returns {boolean}
*/
prot.isValid = function () {
return true;
}
});
}());
masterController.js内容(控制器):
(function() {
var moduleController = angular.module('masterController', [
'ui.bootstrap',
'step',
'config'
]);
moduleController.config(function() {
var Step = angular.module('step').Step;
/**
* @type {Array}
*/
$scope.steps = [
new Step("step 1", "/clientTemplates/notification/step1.html"),
new Step("step 2", "/clientTemplates/notification/step2.html", {test2: 2}),
new Step("step 3", "/clientTemplates/notification/step3.html", {test: 1})
];
};
controller.$inject = ['$scope'];
moduleController.masterController = controller;
console.log(moduleController.masterController);
});
}());
setupMaster.js(应用程序模块)
(function() {
var app = angular.module('setupMaster', [
// 'ngRoute',
//controllers
'masterController',
'config'
]);
/**
* Конфигурационная функция для провайдеров служб приложения
*/
app.config(['$controllerProvider', '$httpProvider', function($controllerProvider, $httpProvider) {
$controllerProvider.register('MasterController', angular.module('masterController').masterController);
}]);
}());
http://docs.angularjs.org/guide/module
在“推荐的设置”块中,写道,应将4个大模块用于服务,指令,过滤器和应用程序层。 控制器或模型工厂/原型怎么样?
也许只是我愚蠢或与Angular的范式不兼容,但是Angular中的模块和注入器系统似乎有点过分设计且违反直觉。 虽然我真的很喜欢Angular的2路数据绑定和脏检查而不是回调。
我正在使用w11k Fabs工作,这是一个基本的grunt设置,其中包含livereload / jshint / sass,less / testing / ngannotate ...
该设置是针对视图/路由的。 表示视图/路线处于分层的犯规结构中。 对于每个视图/路线,都有一个新的模块和ctrl文件。 fabs句柄将它们链接到索引文件中。 由于服务在多个视图中使用,因此它们存储在单独的文件夹中。
如果您“ grunt dist”您的项目,则所有内容都将压缩为1个文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.