2016-06-23 10 views
0

ngResourceをng-repeatと組み合わせて使用​​しており、遅いREST呼び出しでリストが適切に更新されないことに気づいた。それは空のままです。ng-resource呼び出しが終了した後でng-repeatを更新する

私が理解する限り、コントローラとng-repeat要素の間にバインディングが必要です。

私のリソースとコントローラの定義:

(function (configure) { 
    configure('loggingResource', loggingResource); 

    function loggingResource($resource, REST_CONFIG) { 
     return { 
      Technical: $resource(REST_CONFIG.baseUrl + REST_CONFIG.path + '/', {}, { 
       query: { 
        method: 'GET', 
        isArray: true 
       } 
      }) 
     }; 
    } 
})(angular.module('loggingModule').factory); 

(function (configure) { 
    configure('logController', logController); 

    function logController(loggingResource) { 
     var that = this; 
     loggingResource.Technical.query(function (data) { 
      that.logs = data; 
     }); 
     //that.logs = loggingResource.Technical.query(); 
    } 
})(angular.module('loggingModule').controller); 

ng.repeat使用:

私がこれまで試してみました何
<tr class="table-row" ng-repeat="log in logController.logs"> 

  • ng-bindを組み合わせてng-repeat
  • でdeferrerとngResource

  • $promise私は何を逃したのですか? https://plnkr.co/edit/t1c5Pxi7pzgocDMDNITX

  • +0

    'that.logsを追加しようとします= []; 'を呼び出してからAPIを呼び出します。 – MaKCbIMKo

    +0

    何も変更されていない – CSchulz

    答えて

    0

    {{}}はコントローラとビューの間にデフォルトのバインディングを提供し、明示的に何も追加する必要はありません。私は注入された定数などのマイナーな変更を加えてplunkrを更新しています。

    // Code goes here 
    angular.module("loggingModule", ['ngResource']); 
    
    (function(configure) { 
        configure('loggingResource', loggingResource); 
    
        function loggingResource($resource) { 
        return { 
         Technical: $resource('https://api.github.com/users/addi90/repos', {}, { 
         query: { 
          method: 'GET', 
          isArray: true 
         } 
         }) 
        }; 
        } 
    })(angular.module('loggingModule').factory); 
    
    (function(configure) { 
        configure('logController', logController); 
    
        function logController(loggingResource) { 
        var that = this; 
        that.logs = [{ 
         title: 'test2' 
        }]; 
        loggingResource.Technical.query(function(data) { 
         that.logs = data; 
        }); 
        //that.logs = loggingResource.Technical.query(); 
        } 
    })(angular.module('loggingModule').controller); 
    

    APIリソースが働いていなかったので、私はそこに

    を私のGitHubのレポAPIのリンクを使用していた更新作業plunkrは、ここに提供されています:https://plnkr.co/edit/cederzcAGCPVzc5xTeac?p=preview

    +0

    btwこれはリソースですので、 'that.logs = loggingResource.Technical.query( ); ' –

    0

    ので、あなたは、ログのリストを再初期化するために防ぐ

    that.logs.push(data) 
    

    を使用してみてください:plnkrにそれを得るために

    私の試み。ログリストをオーバーライドし、ログリストの前にng-repeatが初期化されていると解決されないようです。 残りの通話後にログリストが正しく設定されていますか?

    +0

    ng-repeatとコントローラの間のバインディングは、角が双方向バインディングを使用するので自動的に行われます – Thorsten

    関連する問題