如何在ng-model上使用$ watch?

时间:2015-05-18 18:56:24

标签: angularjs

一旦用户从下拉树中选择值,我想触发一个事件并调用该服务以获取所选值的描述并将其映射到另一个字段(populatefield.html)。如何通过以下代码实现这一目标......

test.html

<div multiselect-dropdown-tree ng-model="nonPersistentProcess.erhKey"  options="erhTreeviewOptions"></div>

populatefield.html

<input type="text" class="form-control" id="CustSegment"
                                        name="CustomerSegment"
                                        ng-model="processDTO.businessSegmentOrControlFunction"
                                        maxlength="512" disabled="disabled">

Ctrl.js

  $scope.$watch($scope.nonPersistentProcess.erhKey,function(newVal,oldVal){
          if(newVal > oldVal){
            ERhHirachyInfo.getErhInfo($scope.nonPersistentProcess.erhKey).then(function(response){
              $scope.erhObj = response.data;
            });
          }
       });

1 个答案:

答案 0 :(得分:0)

我想出来并通过以下代码使其工作....

 $scope.$watch(function(){return $scope.nonPersistentProcess.erhKey.length},
            function(newVal,oldVal){
              if(newVal > oldVal)
                  {
                    ERhHirachyInfo.getErhInfo($scope.nonPersistentProcess.erhKey[0].id).then(function(response){
                    $scope.erhObj = response.data;
                    });
                  }
       });