AngularJS 1.6:当监视触发时,组件内的标签不会更新

时间:2018-05-10 12:34:05

标签: angularjs angular-components

我的组件中有一块$ watch,它会关注工厂的变化。

这部分有效,我可以看到$ watch注意和回应。

我的问题是我需要让模板用new重新渲染 标签。在我转移到$ watch之前,我可以使用$ apply来轻推页面 重装,但现在似乎没有用。

我还可以插入警报,看到更新的信息肯定会影响组件。

`

mapApp.component("vesselInfo", {
    template : '<div class="vessel-info" >
                  <div ng-bind="viCtrl.title"></div>
                </div>',
     bindings: {   
     },
     controller: VesselInfoController,
     controllerAs: 'viCtrl'
});

function VesselInfoController($scope, $element, $attrs, $http, SelectedVessel) {

    this.title = 'No Vessel Selected';

    // bind the controller property to the service collection
      this.selectedVessel = SelectedVessel.getSelectedVessel();

      // watch the collection for changes
      $scope.$watch(watchSource, function(current, previous){
        this.selectedVessel = current;
        if(current) {
            this.title = 'Name : ' + current.name;
        }
        else {
            this.title = 'No Vessel Selected';
        }           
      });

      function watchSource(){
        return SelectedVessel.getSelectedVessel();
      } 
}

`

1 个答案:

答案 0 :(得分:0)

使用以下代码

尝试
$scope.$watch(watchSource, function(current, previous) {
    // Your watch code here
}, true);

在$ watch调用结束时,你可以传递一个处理objectEquality的第三个参数(参见docs here) 如果您在此处传递true,则会使用angular.equals方法,该方法可以更彻底地检查您的收集中的差异。