Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to apply multiple AngularJS controllers on the same element

Is it possible to apply multiple AngularJS controllers on the same element ?

like image 278
faressoft Avatar asked Feb 20 '15 21:02

faressoft


People also ask

Can we use multiple controller in AngularJS?

An AngularJS application can contain one or more controllers as needed, in real application a good approach is to create a new controller for every significant view within the application.

Can multiple controllers be used in a single HTML page?

Multiple controllers can be used in a single html page, provided they all belong to the same module.

Can we inject one controller into another controller in AngularJS?

You can't inject controllers into one another.

What are the controllers in AngularJS JS?

The controller in AngularJS is a JavaScript function that maintains the application data and behavior using $scope object. You can attach properties and methods to the $scope object inside a controller function, which in turn will add/update the data and attach behaviours to HTML elements.


3 Answers

No, you cannot apply two controllers to the same element, but you can apply multiple directives. And directives can have controllers.

app.directive('myDirective1', function() {
return {
controller: function(scope) {
  //directive controller
  }
 };
});

app.directive('myDirective2', function() {
return {
controller: function(scope) {
  //directive controller
  }
 };
});

and in the HTML:

<div myDirective1 myDirective2></div>

And as mentioned in the comments below, the two controllers could share the same scope, which is often the desired effect; one of the two controller can request a new scope, but you cannot have two new scopes;

the reason for not allowing two isolated scope on the two directives, is that the view would not know where to get the scope values from, if a scope variable had the same name in the two isolated directive controllers

Here is an interesting read: Why can't multiple directives ask for an isolated scope on the same element?

like image 121
Manube Avatar answered Oct 17 '22 02:10

Manube


You could extend a controller and use it wherever you like. See the Fiddle for a better example.

<script>
var multiApp = angular.module('new', []);

multiApp.controller('aCtrl', ['$scope', '$controller', function ($scope, $controller) {
  $scope.text1 = 'Hello';

  angular.extend(this, $controller('bCtrl', {$scope: $scope}));
}]);



multiApp.controller('bCtrl', ['$scope', function ($scope) {
                $scope.text2 = 'World!';
}]);
</script>

With html like:

<body ng-app="new">
    <div id="container1" ng-controller="aCtrl">
        {{text1}} {{text2}}
    </div>
</body>

Fiddle: http://jsfiddle.net/kkelly/thk9n20o/#base.com

like image 24
KellyCode Avatar answered Oct 17 '22 00:10

KellyCode


No

HTML is a form of XML, and it is not valid xml to have multiple non-unique attributes on the same element. In other words,

<div ng-controller="a" ng-controller="b">

is invalid. But what about when we do

<div id="a" ng-controller="a">
    <div id="b" ng-controller="b">
        <div id="c">

This is valid xml/HTML, but it is not assigning two controllers to the div with id c. This is called Nested Controllers.

like image 8
David says Reinstate Monica Avatar answered Oct 17 '22 02:10

David says Reinstate Monica