Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias the AngularJS $scope object in a controller

Without getting into the details of why, I need to provide an alias for $scope in my controllers. Instead of injecting and decorating $scope I'd like for the users to instead be able to inject view and have it have the same effect.

Based on my understanding of angular, $scope is created by a $scopeProvider which is a factory registered at configuration time of the angular app. I'm assuming that I need to register a viewProvider and set it equal to the $scopeProvider but I haven't had luck with what I've been trying. Any ideas?

FYI: I'm not looking for something like ['$scope', function(view){..., the ideal solution would work with ['view', function(view){.... The view object would act exactly like $scope, two way binding, etc.

like image 279
Arbiter Avatar asked Apr 03 '15 00:04

Arbiter


Video Answer


1 Answers

If you are teaching AngularJS, you should actually avoid using $scope and use controller as syntax -- See this awesome Angular Style Guide by John Papa for explanation.

That would also solve your problem :)

like image 103
Dmitri Zaitsev Avatar answered Oct 11 '22 02:10

Dmitri Zaitsev