O
O
odd-look2014-12-08 12:10:15
Angular
odd-look, 2014-12-08 12:10:15

This or scope in AngularJS?

Actually the question is in the title. I know the difference, but which is better to use?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mikhail Osher, 2014-12-08
@odd-look

It is more desirable - scope, because not always directives will be able to have access to the controller.
And to scope - always.

A
Alexander, 2014-12-08
@OneFive

Least

var self = this;
this.a = function() {
    self.b = 'c';
} // или  .bind(this)

// vs 

$scope.a = function() {
    $scope.b = 'c';
}

T
TekVanDo, 2014-12-08
@TekVanDo

Personally, I use this. The main advantage is the ability to use Controller as Sintax. Which makes html much clearer.

<div ng-contoller="parentController">
     <div ng-contoller="childController">
         <!--Невозможно понять в каком контроллере находиться test-->
         {{test}}  
     </div>
</div>

Instead of:
<div ng-contoller="parentController as parent">
     <div ng-contoller="childController as child">
         <!--Видно откуда вызывается test-->
         {{parent.test}}  
     </div>
</div>

V
Vladimir Gomonov, 2014-12-11
@MetaDriver

I use both: this for direct access to controller instances (see TekVanDo 's answer above), and $scope otherwise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question