AngularJS controller and service creation

Brad8118

Im working on a new Angular project with the following files:

//app.js
var mainApp = angular.module('mainApp', ['serviceA', 'serviceB', "ctrlA", 'ctrlB']);

//ctrlA.js
var ctlA = angular.module('serviceA', []);
ctlA.controller('menuController', ['$scope', 'serviceA', function($scope, serviceA) {
  // ...
}]);

Is there a reason not to do it this way ?

//ctrlB.js
angular.module('mainApp')
.controller('ctrlB', ['$scope', 'serviceA', function($scope, serviceA) {
  // ...
}]);
Aviro

What Zenorbi, mentioned is absolutely right. However, thought of adding this information to conclude a better answer.

You can modularize the app into multiple segments to make it easy to understand. The segmentation can be done in different ways.

Based on service types - this will be useful for small applications

var mainApp = angular.module('mainApp', [
    'app.controllers',      // Controllers
    'app.directives',       // Directives
    'app.filters',          // Filters
    'app.services'          // Services
]);

Example implementation of a module.

var ctrls = angular.module('app.controllers');
ctrls.controller('menuController', ['$scope', 'serviceA', function($scope, serviceA) {
  // ...
}]);

According to the purpose of each module. For example, you can separate chart related classes to a separate module and isolate it from the main code base. This will allow parallel programming by someone else, as this module can act independent from the parent module.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

AngularJs controller, wait for service $http result

来自分类Dev

AngularJS:从Controller或Service进行angular-translate设置语言

来自分类Dev

Inject AngularJS 1.2 $location service into controller not working in PhoneGap 3.0

来自分类Dev

通过Service调用$ http,然后将数据拉入Controller(AngularJS)

来自分类Dev

当AngularJS中的Controller和Service之间出现某些错误时,无法调用API Services?

来自分类Dev

@Controller @Service @Repository的不足

来自分类Dev

AngularJS - Directive not recognizing controller

来自分类Dev

模板中的AngularJS“ Controller as”

来自分类Dev

AngularJS controller return value

来自分类Dev

Multiple connections on the controller service (Spring)

来自分类Dev

How to separate Controller file in angularjs

来自分类Dev

AngularJS:“ Controller as”语法和指令

来自分类Dev

AngularJS'controller as'和form。$ valid

来自分类Dev

在Controller AngularJS中创建函数

来自分类Dev

将实例变量从Controller移到Service Class

来自分类Dev

AngularJS将更改的值从Controller传递到Directive Controller

来自分类Dev

如何将$ scope从Controller转发到Controller AngularJS?

来自分类Dev

AngularJS将更改的值从Controller传递到Directive Controller

来自分类Dev

AngularJS Controller作为语法和指令

来自分类Dev

AngularJs-使用外部controller.js

来自分类Dev

Angularjs中从Diretives到Controller的通信

来自分类Dev

Angularjs ns-style change via Controller

来自分类Dev

AngularJS,$ http.get()和“ controller as”

来自分类Dev

AngularJS在Controller中刷新数据的正确方法

来自分类Dev

AngularJS指令内的ng-controller?

来自分类Dev

通过Controller AngularJS ns样式更改

来自分类Dev

AngularJS ng-controller不起作用

来自分类Dev

Notify AngularJS Controller about events from outside

来自分类Dev

如何在angularjs中分离Controller文件

Related 相关文章

  1. 1

    AngularJs controller, wait for service $http result

  2. 2

    AngularJS:从Controller或Service进行angular-translate设置语言

  3. 3

    Inject AngularJS 1.2 $location service into controller not working in PhoneGap 3.0

  4. 4

    通过Service调用$ http,然后将数据拉入Controller(AngularJS)

  5. 5

    当AngularJS中的Controller和Service之间出现某些错误时,无法调用API Services?

  6. 6

    @Controller @Service @Repository的不足

  7. 7

    AngularJS - Directive not recognizing controller

  8. 8

    模板中的AngularJS“ Controller as”

  9. 9

    AngularJS controller return value

  10. 10

    Multiple connections on the controller service (Spring)

  11. 11

    How to separate Controller file in angularjs

  12. 12

    AngularJS:“ Controller as”语法和指令

  13. 13

    AngularJS'controller as'和form。$ valid

  14. 14

    在Controller AngularJS中创建函数

  15. 15

    将实例变量从Controller移到Service Class

  16. 16

    AngularJS将更改的值从Controller传递到Directive Controller

  17. 17

    如何将$ scope从Controller转发到Controller AngularJS?

  18. 18

    AngularJS将更改的值从Controller传递到Directive Controller

  19. 19

    AngularJS Controller作为语法和指令

  20. 20

    AngularJs-使用外部controller.js

  21. 21

    Angularjs中从Diretives到Controller的通信

  22. 22

    Angularjs ns-style change via Controller

  23. 23

    AngularJS,$ http.get()和“ controller as”

  24. 24

    AngularJS在Controller中刷新数据的正确方法

  25. 25

    AngularJS指令内的ng-controller?

  26. 26

    通过Controller AngularJS ns样式更改

  27. 27

    AngularJS ng-controller不起作用

  28. 28

    Notify AngularJS Controller about events from outside

  29. 29

    如何在angularjs中分离Controller文件

热门标签

归档