2

I want to know, how to get scope by scope.$id. I know that I can get all scopes in page and then I can find that one scope, but I looking for simpler implementation.

For instance:
1. I want to get scope which id is 100 from $rootScope:

$rootScope.$getScope(100)

2. From current $scope or variable angular

$scope.$getScope(100)
angular.scope(100)

Can I get specified scope in that way?

A . Radej
  • 41
  • 5

1 Answers1

0

Answer form comments is using jQuery. If you want plain javascript, more functional approach, you could do:

function getScope(scopeId){
     return Array.from(document.querySelectorAll(".ng-scope"))
     .map(el => angular.element(el).scope())
     .filter(scope => scope.$id == scopeId)[0]
}

$rootScope.getScope = getScope;    

As far as I know there is no other way to find all scopes on page than just query for .ng-scope.

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
  • Hmm.. okej, but It also based on DOM elements, it's similar to first solution @SaEChowdary (link is below question [link](http://stackoverflow.com/questions/23253506/get-dom-element-by-scope-id/23255103#23255103)), but I'm looking for solution without using DOM elements. – A . Radej Dec 20 '16 at 12:10