vtortola
vtortola

Reputation: 35925

How to extend $scope globally?

I would like that all my $scope objects contain a specific helper method that I use to retrieve data like $scope.getData('member.submember.othermember') otherwise I would have to do $scope['member']['submember']['othermember'].

The idea is to add this .getData method to all the created $scope objects by default.

Is there any extensibility point where I may add that?

Upvotes: 1

Views: 142

Answers (1)

Estus Flask
Estus Flask

Reputation: 222528

Angular scopes use JS prototypal inheritance, it is possible to add custom method to all scopes (in addition to existing $ methods) with

$rootScope.constructor.prototype.$getData = function () { ... };

Upvotes: 2

Related Questions