繁体   English   中英

角度代码结构的最佳实践

[英]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路数据绑定和脏检查而不是回调。

我一直在寻找最佳实践和理由。 除了阅读有角度的文档外,其他文章..我发现JohnPapa的描述和深入的解释使您对为什么和为什么不做什么,不应该做什么有更多的了解。 在这里阅读

像其他建议的一样,请检查角度种子和其他ng生成器等。最终,您会随着时间的推移适应正确的做法。

没有对与错,因为它们都有正确的观点。 即。 可测试性,依赖项注入,最小化,优化等。

只是分享..

我正在使用w11k Fabs工作,这是一个基本的grunt设置,其中包含livereload / jshint / sass,less / testing / ngannotate ...

该设置是针对视图/路由的。 表示视图/路线处于分层的犯规结构中。 对于每个视图/路线,都有一个新的模块和ctrl文件。 fabs句柄将它们链接到索引文件中。 由于服务在多个视图中使用,因此它们存储在单独的文件夹中。

如果您“ grunt dist”您的项目,则所有内容都将压缩为1个文件。

尝试: https : //www.npmjs.com/package/fabs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM