Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require a controller in an angularjs directive

Can anyone tell me how to include a controller from one directive in another angularJS directive. for example I have the following code

var app = angular.module('shop', []). config(['$routeProvider', function ($routeProvider) {     $routeProvider.when('/', {         templateUrl: '/js/partials/home.html'     })         .when('/products', {         controller: 'ProductsController',         templateUrl: '/js/partials/products.html'     })         .when('/products/:productId', {         controller: 'ProductController',         templateUrl: '/js/partials/product.html'     }); }]);  app.directive('mainCtrl', function () {     return {         controller: function ($scope) {}     }; });  app.directive('addProduct', function () {     return {         restrict: 'C',         require: '^mainCtrl',         link: function (scope, lElement, attrs, mainCtrl) {             //console.log(cartController);         }     }; }); 

By all account I should be able to access the controller in the addProduct directive but I am not. Is there a better way of doing this?

like image 780
Subtubes Avatar asked Mar 28 '13 01:03

Subtubes


People also ask

What AngularJS directive defines a controller?

The ng-controller directive adds a controller to your application. In the controller you can write code, and make functions and variables, which will be parts of an object, available inside the current HTML element. In AngularJS this object is called a scope.

Which directive is used for controller in angular?

Controllers can be attached to the DOM in different ways. For each of them, AngularJS will instantiate a new Controller object, using the specified Controller's constructor function: the ngController directive.

How pass data from controller controller to AngularJS?

Approach: To share data between the controllers in AngularJS we have two main cases: Share data between parent and child: Here, the sharing of data can be done simply by using controller inheritance as the scope of a child controller inherits from the scope of the parent controller.

How do you access the directive variable in a controller?

You just create a myVar variable in your controller and pass it to the directive using my-var attribute. Since you are using two way binding, any changes made to myVar by the directive are available in your controller.


1 Answers

I got lucky and answered this in a comment to the question, but I'm posting a full answer for the sake of completeness and so we can mark this question as "Answered".


It depends on what you want to accomplish by sharing a controller; you can either share the same controller (though have different instances), or you can share the same controller instance.

Share a Controller

Two directives can use the same controller by passing the same method to two directives, like so:

app.controller( 'MyCtrl', function ( $scope ) {   // do stuff... });  app.directive( 'directiveOne', function () {   return {     controller: 'MyCtrl'   }; });  app.directive( 'directiveTwo', function () {   return {     controller: 'MyCtrl'   }; }); 

Each directive will get its own instance of the controller, but this allows you to share the logic between as many components as you want.

Require a Controller

If you want to share the same instance of a controller, then you use require.

require ensures the presence of another directive and then includes its controller as a parameter to the link function. So if you have two directives on one element, your directive can require the presence of the other directive and gain access to its controller methods. A common use case for this is to require ngModel.

^require, with the addition of the caret, checks elements above directive in addition to the current element to try to find the other directive. This allows you to create complex components where "sub-components" can communicate with the parent component through its controller to great effect. Examples could include tabs, where each pane can communicate with the overall tabs to handle switching; an accordion set could ensure only one is open at a time; etc.

In either event, you have to use the two directives together for this to work. require is a way of communicating between components.

Check out the Guide page of directives for more info: http://docs.angularjs.org/guide/directive

like image 72
Josh David Miller Avatar answered Oct 09 '22 03:10

Josh David Miller