当范围发生变化时,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
}
});

谢谢!

我没有看到使用if/else,因为您希望在$scope.format更改时使用该newValue调用服务方法。

所以它可以像这样做:

$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;
});
}
});

将服务包装在一个函数周围,并在监视函数中调用它

$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
}
});

最新更新