2017-05-30 13 views
0

私は工場出荷時に条件付きのデータを取得しています。私が欲しいのは、条件を変えて工場を更新する必要があるときです。スコープ変更時にAngualrjが工場から値を取得

$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

? – anoop

+1

ハハはそれを手に入れました。ありがとう男 –

答えて

1

機能の周りのサービスをラップし、あなたがnewValueたび$scope.format変更でサービスメソッドを呼び出すようにしたいので、私は、if/elseの使用が表示されていないウォッチ関数内

$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 nice。 D –

2

それを呼び出します。

だから、のように行うことができます://が、なぜならば/他、.. *新しい値で、ここでget_donwload_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

関連する問題