2017-05-30 43 views
0

我有工厂用于获取具有条件的数据。我想要的是什么时候换工厂也需要更新。当范围更改时,Angualrjs从工厂获得价值

$scope.format=1;  
homefact.get_download_format($scope.format).then(function (response) { 
     $scope.download = response; 
    }); 

//watch scople format 
    $scope.$watch("format", function (newValue, oldValue) { 
     if ($scope.format === 1) { 
     //recall the get_donwload_format here with new value 

     } else { 
      //recall the get_donwload_format here with new value 
     } 
    }); 

谢谢!

+0

同时在地方,你想* //这里回顾一下get_donwload_format新的价值。*,那么为什么的if/else? – anoop

+1

哈哈明白了。感谢人 –

回答

1

包裹服务周围的函数,调用它的手表功能

$scope.format=1;  
callDownload($scope.format); 

function callDownload(newValue){ 
homefact.get_download_format(newValue).then(function (response) { 
    $scope.download = response; 
}); 
} 

$scope.$watch("format", function (newValue, oldValue) { 
     if ($scope.format === 1) { 
     callDownload(newValue) 

     } else { 
      //recall the get_donwload_format here with new value 
     } 
}); 
+1

的作品。谢谢你,如果还有其他问题,我会尽快删除。 –

+0

@Vuong Tran很好。如果这有帮助,请确保检查为答案:D –

2

里面我看不到的if/else使用,因为你要调用与newValue每当$scope.format变化的服务方法。

所以这是可以做到这样的:

$scope.format=1;  
    homefact.get_download_format($scope.format).then(function (response) { 
     $scope.download = response; 
    }); 

    //watch scople format 
    $scope.$watch("format", function (newValue, oldValue) { 
    if(newValue != oldValue && newValue) { 
     homefact.get_download_format(newValue).then(function (response) { 
      $scope.download = response; 
     }); 
    } 
    }); 
+1

您的回答很好,但我们需要复制并粘贴整个get_downlaod_format函数。不是一个好主意。 –

+0

IMO它只是一个调用,并在一个点使用,所以没关系..但如果在多个地方使用,然后将其包装在另一个函数。 – anoop