4

Let's say I have two directives:

angular.module('example').directive('dir1', function () {
  return {
    restrict: 'A',
    scope: true,
    controller: 'ExampleCtrl'
};});

angular.module('example').directive('dir2', function () {
  return {
    restrict: 'A',
    scope: true,
    controller: 'ExampleCtrl'
};});

Is it possible to tell which directive initialized ExampleCtrl inside this controller?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
brisky
  • 197
  • 1
  • 1
  • 14

2 Answers2

1

I thought of 2 ways. This solution I find much less hacky, but is also less dynamic:

angular.module('example').directive('dir1', function () {
  return {
    restrict: 'A',
    scope: true,
    controller: 'ExampleCtrl',
    link: function(scope) {
      $scope.myDir="dir1";
    }
};});

angular.module('example').directive('dir2', function () {
  return {
    restrict: 'A',
    scope: true,
    controller: 'ExampleCtrl'
    link: function(scope) {
      $scope.myDir="dir2";
    }
};});

In this way you are baking the parent directive into the scope, which is available to the controller.


This solution is more hacky, but also more dynamic. Using this question, we can get our containing element:

function innerItem($scope, $element){
    var jQueryInnerItem = $($element); 
}

from there, we can test that element for attributes and properties that would be specific to one directive over another (such as directive name). I still think this is very hacky and you have an underlying issue (perhaps a bit of XY problem here), but this should work.

Community
  • 1
  • 1
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • Your main solution will not work for me, because I need to identify directive in controller initialization code and link is executed only after controller is initialized – brisky Apr 24 '15 at 16:30
  • @brisky Why do you need it during initialization? These requirements are getting a bit silly. You seem to have a bigger underlying problem with your design at this point.That being said, you can look into compile to get the values in pre-initialization... but again you really need to take a hard look at your design. – David says Reinstate Monica Apr 24 '15 at 16:36
  • I don't want to post the exact problem that I am solving because I know there are easier ways to solve it. What I am trying to achieve with my current solution is better readability in my html templates by using directives with values as simple strings and not objects – brisky Apr 24 '15 at 16:45
  • @brisky These solutions are not going to improve readability... Just use your easier solution. Easier = simpler = better. – David says Reinstate Monica Apr 24 '15 at 16:47
1

You can do this:

angular.module('example').directive('dir1', function () {
  return {
    restrict: 'A',
    scope: true,
    controller: 'ExampleCtrl',
    controllerAs: 'dir1Ctrl'
};});

angular.module('example').directive('dir2', function () {
  return {
    restrict: 'A',
    scope: true,
    controller: 'ExampleCtrl',
    controllerAs: 'dir2Ctrl'
};});

Then, in the controller, $scope.dirNcontroller, where N is 1 or 2 or as many as you have, will exist only if it came from directive N. This is how the controllerAs syntax works, it is simply published to the scope.

Something like this:

app.controller('ExampleCtrl', function($scope, $element) {
  if ($scope.dir1Ctrl) /* do something */
  if ($scope.dir2Ctrl) /* do something else */
});

PLUNKER

Mosho
  • 7,099
  • 3
  • 34
  • 51