PatrickJS / NG6-starter

:ng: An AngularJS Starter repo for AngularJS + ES6 + Webpack
https://angularclass.github.io/NG6-starter
Apache License 2.0
1.91k stars 1.35k forks source link

I must use $apply() to see changes...? #203

Closed edin-m closed 7 years ago

edin-m commented 7 years ago

I have simple component/controller/html for "file manager" so:

html:

  <div>
    {{ ($ctrl.wickedSick || {}) | json }}
  </div>

component:

export default {
  restrict: 'E',
  bindings: {},
  template,
  controller
};

and controller:

class FileManagerController {
  constructor($scope) {
    this.wickedSick = 'wicked sick !!! 123';
    setTimeout(() => {
      this.wickedSick = 'new value';
      $scope.$apply();
    }, 5000);
  }
}

If I don't have $scope.$apply() I don't se resulting changes. And I would like not to use $apply() manually... Am I doing something wrong?

fesor commented 7 years ago

you should wrap all async functions to make them start digest cycle. In case of setTimeout angular provides you $timeout service which handles digest for you.