How to watch component binding change using Angular component

前端 未结 5 2046
醉梦人生
醉梦人生 2020-12-14 15:04

How can I listen to angular component binding change and perform actions?

angular.module(\'myapp\')
    .component(\'myComponent\', {
        templateUrl: \'         


        
5条回答
  •  一整个雨季
    2020-12-14 15:22

    I've discovered a way but not sure it's the most efficient. First bring in $scope as a dependency and set it to this._scope or the like in your constructor. I have the following then in my $onInit function:

    this._scope.$watch(() => {
        return this.items;
      },
      (newVal, oldVal) => {
        // Do what you have to here
      });
    

    It's highly inspired by the answer here: Angularjs: 'controller as syntax' and $watch

    Hope it helps, it's what I'm going to use until I'm told otherwise.

提交回复
热议问题