正确使用Angular MVC设置

随意清除你在这里看到的任何误解。 我不是前锋。

现在,我已经读了很多逻辑不应该存在于控制器中,而应该放在其他地方。 但是大多数地方我看这个显示代码没有指定它属于哪个文件。在我inheritance的这个项目中,我有4个文件处理主要function:

  • 一个控制器 – autoDeploy.js
  • 服务 – autoDeploy.service.js
  • 一个模块 – autoDeploy.module.js
  • 指令 – directives.js

directives.js只是包含我想要注入到DOM的模板,点击button后,指令将在稍后引用。

autoDeploy.module.js执行所有的module.config$stateProvider路由的东西; 我没有触及它超出了我最初的修改指向我正在创build的新页面,所以它可以正确路由到。

autoDeploy.service.js :在我看到的例子中, .factory()的(或.service() )最后一个参数通常作为一个函数打开,文件中的所有function都发生在那里。 我不是那样的,这个工厂是一个独立的命令,接着是一个看起来像构造函数的工厂。 这是我有:

 (function () { //start iife 'use strict'; angular.module('gms.autoDeploy') .factory('AutoDeployService', ["$http", "$q", "$log", "$cookies", "APP_CONFIGS", "SweetAlert", "$timeout", "GridSettingsService", "APP_USER", AutoDeployService]); function AutoDeployService($http, $q, $log, $cookies, APP_CONFIGS, $timeout, SweetAlert, GridSettingsService, APP_USER) { //return service object at bottom of function function returnOne() { return "one"; } var service = { returnOne: returnOne }; return service; } //end AutoDeployService }()); //end iife 

为什么最初的开发者…

  • 作为生活用途?
  • 使其成为函数映射后返回servicevariables?
  • 把所有的function放在像函数的构造函数中?

我对上述第二和第三个答案的猜测是它是服务的构造函数,而控制器可以知道它是一个可用的对象,因为我们把它作为parameter passing给控制器​​,就像你在顶层看到的那样下面的代码。 我也不太了解“构造函数”的参数,但我可以稍后再看。

现在控制器。 与上面的服务不同,控制器声明在.controller()的参数中作为函数打开,就像我见过的其他地方一样。 这是简化的(具有类似function的方法被删除):

 angular.module("gms.autoDeploy").controller('AutoDeployController', ['$scope', '$compile', 'AutoDeployService', function ($scope, $compile, AutoDeployService) { var vm = this; init(); function init() { vm.isInstantiated = true; vm.data = { "parameter": [] }; } // calling a function from the service to show that we can pass // data from controller to service vm.change = function () { vm.message = AutoDeployService.returnOne("not one"); }; //function assigned to button click on the DOM allowing // the directive to inject the template where the <tib-copy> tag is vm.addCopy = function (ev, attrs) { var copy = angular.element(document.createElement('copy')); var el = $compile(copy)(vm); angular.element(document.getElementsByTagName("tib-copy")).append(copy); vm.insertHere = el; }; // This method extracts data from the following fields dynamically added by the click of a button: // - TIBCO Server(s) // - TIBCO Domain(s) // - TIBCO Application(s) vm.getTibPromotionData = function () { // Add all TIBCO servers var servers = document.getElementsByName("tibServer"); var tibSvrList = []; for (var i = 0; i < servers.length; i++) { tibSvrList.push(servers[i].value); } // Add all TIBCO domains var domains = document.getElementsByName("tibDomain"); var tibDomainList = []; for (i = 0; i < domains.length; i++) { tibDomainList.push(domains[i].value); } // Add all applications var tibApps = document.getElementsByName("tibApp"); var tibAppList = []; for (i = 0; i < tibApps.length; i++) { tibAppList.push(tibApps[i].value); } // Add the processed data to the final JSON vm.data.parameter.push({ "name": "TIBCO_Promotion", "value": JSON.stringify("[{\"server\":[" + tibSvrList + "]},{\"domain\":[" + tibDomainList + "]},{\"application\":[" + tibAppList + "]}]") }); }; } ]); 

请让我知道,如果你看到在控制器中应该属于autoDeploy.service.js文件的任何东西。 此外,如果任何人有这种文件命名约定的经验,我很想听到为什么有文件名为*.service.js*.module.js ,如果*.service.js文件有任何与服务和工厂的概念有关,或者如果它的目的是概念化的,就好像它只是作为后端服务组件的参考。