0

Given two scopes - x, y - is there a built in function that returns true if x is an ancestor of y?

(I can obviously traverse from y to the $rootScope using $parent and compare $id along the way)

EDIT:

In the meanwhile I'm using something like this:

function isChildScope(parentScope, childScope) {
    while (childScope) {
        if (parentScope.$id === childScope.$id) {
            return true;
        }
        childScope = childScope.$parent;
    }
    return false;
};
seldary
  • 6,186
  • 4
  • 40
  • 55
  • May be this could answer this: http://stackoverflow.com/a/13428220/1059101 – Jai Sep 04 '14 at 12:19
  • @Jai - That answer has nothing to do with my question... I'm not trying to access a child scope, just figure out if one scope is an ancestor of another. – seldary Sep 04 '14 at 12:23
  • So do you want it in code or angular chrome extension named batarang would surely help you. – Jai Sep 04 '14 at 12:25
  • @Jai I switched from batarang to [ng-inpector](https://chrome.google.com/webstore/detail/ng-inspector-for-angularj/aadgmnobpdmgmigaicncghmmoeflnamj) I find it not as buggy and much more detailed. – Malkus Sep 04 '14 at 12:29
  • In code. And by the way, batarang doesn't support latest versions of angular. – seldary Sep 04 '14 at 12:29

2 Answers2

1

There is no built-in method on $scope in the source, so I doubt it's somewhere else. You probably can compare $id as you said or simply x.$parent === y to check that.

domakas
  • 1,246
  • 8
  • 9
0

If you are looking to clearly identify which controller a $scope belongs to I would recommend Controller As

<!-- In Your Binding -->
<div ng-controller="MyCtrl as ctrl">
   <span>{{ctrl.foo}}</span>
</div>

<!-- In Your Controller -->
app.controller('MyCtrl', function() {
     this.foo = "Hello World!";
});

This syntax will double bind the controller but will make the $scope you are working with clearly defined.


Here is a nice example with nested controllers that show how readability is improved.

<div ng-controller="MainCtrl as main">
  {{ main.title }}
  <div ng-controller="AnotherCtrl as another">
    Scope title: {{ another.title }}
    Parent title: {{ main.title }}
    <div ng-controller="YetAnotherCtrl as yet">
      Scope title: {{ yet.title }}
      Parent title: {{ another.title }}
      Parent parent title: {{ main.title }}
    </div>
  </div>
</div>
Malkus
  • 3,686
  • 2
  • 24
  • 39
  • 1
    Thanks, but this is not what i'm looking for. – seldary Sep 04 '14 at 12:22
  • _if x is an ancestor of y_...is this answers it? – Jai Sep 04 '14 at 12:22
  • Sorry @seldary, That is really the best answer I have for this. The reason I suggest this is because it forces you to set values in the proper scope instead of depending on parent scope. – Malkus Sep 04 '14 at 12:26
  • No - suppose you are writing a utility aimed for testing relations between scopes (for tests, performance, etc..). Moreover, you might have scopes created by third party plugins, where you don't have access to their templates. – seldary Sep 04 '14 at 12:26
  • In no case am I depending on a parent scope. – seldary Sep 04 '14 at 12:27