1

I have two DIVs, each one is a self-contained user control or partial view, if I want one team to work on dog div, the other team work on fox div. can each team have their own angular module, controller, view, etc ? If yes, can you show me a code snippet?

another question: if I want to these two DIVs loosely coupled, what is the best angular way to let them communicate ?

<body ng-app>
  <div id="dog">
    <input type="text" ng-model="name"/> {{name}}
  </div>
  <div id="fox">

  </div>
</body>

Thank you!


For other new ng developer's reference, this is the final code, if you have better solution, please feel free to improve it.

<body ng-app="airborneApp">
    <div id="dog" ng-controller="dogController">
        <input type="text" ng-model="name" /> {{name}}
    </div>
    <div id="fox" ng-controller="foxController">
        <input type="text" ng-model="name" /> {{name}}

    </div>

    <script>
        angular.module('airborneApp', ["dogCompany", "foxCompany"]);

        angular.module('dogCompany', []).
            controller('dogController', ['$scope', function ($scope) {
                $scope.name = 'hello dog';
            }]);

        angular.module('foxCompany', []).
            controller('foxController', ['$scope', function ($scope) {
                $scope.name = "hello fox";
            }]);

    </script>
</body>
nandin
  • 2,549
  • 5
  • 23
  • 27

2 Answers2

2

each div can have a separate controller using:

<div ng-controller="firstCtrl"></div>
<div ng-controller="secondCtrl"></div>

for the other part of your question see:

What's the correct way to communicate between controllers in AngularJS?

Community
  • 1
  • 1
Jake Johnson
  • 1,856
  • 1
  • 17
  • 26
2

You can make as many modules as you can, you just have to reference all of them as a dependency in you main App module definition (and load them in correct order)

app

angular.module('myApp', ['firstModule', 'secondModule'])

modules

angular.module('firstModule', []) // empty array as a second parameter creates new module instead of using existing one
    .controller(...)
    .directive(...)

angular.module('secondModule', [])
    .controller(...)
    .directive(...)

For communication between different modules, the simplest way is to inject $rootScope into all controllers.

But preferred way is to create a service in main app module, which will be injected into both modules

angular.module('myApp')
    .factory('SharedData', function() {
        var a = {},
            b = {};

        return {
            a: a,
            b: b
        }
})

and then use it

angular.module('firstModule')
    .controller('something', function($scope, SharedData) {
        SharedData.a.data = 'new data';
})
angular.module('secondModule')
    .controller('something else', function(SharedData) {
        console.log(SharedData.a.data);  //will output 'new data'
})
doodeec
  • 2,927
  • 19
  • 23
  • how do you apply 'firstModule' on html div ? – nandin Mar 21 '14 at 13:49
  • you don't, you create a directive within `firstModule` and in html you just use that directive... it's not so strictly modular in markup, once you load it, you can use it anywhere in html – doodeec Mar 21 '14 at 13:57