0

私は、スコープバインディングに関連する問題がほとんどない角度のあるソーシャルアプリに取り組んでいます。ユーザーのサブスクライバーをリストする http サービスからのスコープ データがあります。検索ユーザーが任意のユーザーを検索し、彼を追加したいときはいつでも、この時点で http 呼び出しを行い、購読者リストの更新されたデータを返します。ただし、ページを更新しないとビューが更新されません。これには interval を使用できますが、定期的な http 要求を行うことでサーバーにより多くの負荷がかかるため、それが解決策になるとは思いません。

JS

 this.loadsubscribers = function (){
 myService.subscriber().then(function(response){
     $scope.subscriber = response.data;         
    });
 }

 // i can make interval call for getting subscriber list but this will put load to my server.  

 $interval(function(){
  this.loadsubscribers();
 }.bind(this), 10000);   

this.loadsubscribers();

ユーザーがサブスクライバーの追加ボタンをクリックしたときに、このスコープを更新する必要があります。ここで、ユーザーがサブスクライバーの追加をクリックすると、checkit関数が呼び出され、http call を作成してそのユーザーをサブスクライバーリストに追加します。データを保存した後、購読者の現在のリストを http success に返します。しかし、それはビューを更新していません。

$scope.checkit = function(value,id,firstname,str){      

    if(text === 'get along'){
    $http.post(baseurl+"ajax/subscriber_post", "subscriber_id="+value).success(function(data){
       //here i have to update scope subscriber and 
       // refresh view  as you can see i am doing it but it doesn't update view.  
      $scope.subscriber =data; 
      growl.addSuccessMessage( firstname+" is your fellow now");    
      $scope.datatocheck.push(value);
      $scope.Checkfriend(value); 
    });
}

間隔は機能していますが、使用したくありません。データが同期更新を必要としない場合、これを行う最善の方法は何でしょうか。

4

2 に答える 2

0

この行を変更します。

$scope.subscriber =data; 

これに:

$scope.$apply(function() {
    $scope.subscriber =data; 
});
于 2014-06-03T07:17:05.290 に答える
0

これを試して:

$scope.$eval(function() {
    $scope.subscriber = data;
});

それでもダイジェスト ループの問題が発生する場合は、$evalAsync代わりに を試してください$eval

于 2014-06-03T09:26:24.980 に答える