0

Now I write code without context, $scope

app.controller(function(){
  // ..
  this.list = [];
  // what ever work with a list of

  this.$digest(); // is not a function, why?


});

before I could

app.controller(function($scope){
  // ..
  $scope.list = [];
  // what ever work with a list of

  $scope.$digest();


});

But me need this, how it work?

splincode
  • 183
  • 2
  • 13
  • 1
    just because you use `this` doesn't mean you can't inject `$scope`. – Claies Nov 09 '16 at 17:54
  • 1
    here is an answer I wrote a while back to try to explain the concept: http://stackoverflow.com/a/30644370/2495283 – Claies Nov 09 '16 at 17:55
  • $scope considered bad practise – splincode Nov 09 '16 at 17:56
  • 2
    **storing values *directly*** on `$scope` is considered bad practice by **some**, however using `$scope` for what it is intended for is never a problem. – Claies Nov 09 '16 at 17:57
  • @Claies is that a canonical answer? Do you consider this question a duplicate? – simbabque Nov 09 '16 at 17:57
  • I would say yes, the answer should be a match to this question. – Claies Nov 09 '16 at 17:58
  • how use $digest in this? – splincode Nov 09 '16 at 17:59
  • 1
    it isn't. `$digest` is a function of `$scope`, and therefore, if you need to call `$digest`, you need to inject `$scope`. `this` points to the controller; using `this` (ControllerAs) instead of `$scope` creates an alias of the controller on `$scope`, but doesn't expressly give the controller access to `$scope` methods. `$scope` is a provider that does a lot, and when people say "dont' use `$scope`", they probably should be saying "don't use `$scope` when it isn't necessary". It is *necessary* if you need `$digest`. – Claies Nov 09 '16 at 18:02
  • 1
    as a side point, there are very few reasons to ever need `$digest`, and if you are so against `$scope` that you are arguing against it here, it's surprising that you aren't trying to find a way not to use `$digest`. – Claies Nov 09 '16 at 18:03
  • here is another more in depth analysis of the same topic, maybe this can explain it better (differently, at least). http://stackoverflow.com/a/14168699/2495283 – Claies Nov 09 '16 at 18:10

0 Answers0