2016-04-27 24 views
0

Googleスプレッドシートからオブジェクトを配列にロードしています。しかし、私はこれらのオブジェクトを自動的に表示するときに、ページが読み込まれます。問題は、それらのオブジェクトをロードするには2秒か2秒かかります。私のページは初期化が完了する前に完全にロードされています。この方法では、コントローラをもう一度呼び出すまでオブジェクトを表示しません。だから私の質問です:初期化が完了した後、ng-initで関数を呼び出せますか?AngularJS:間隔の後にng-initを実行する方法

初期化:

this.partsInit = function(){ 
     jQuery.getJSON(this.ssl).success(function(data) { 

      for (var i = 0; i < data.feed.entry.length; i++) { 
       var id = data.feed.entry[i].gsx$id.$t; 
       var name = data.feed.entry[i].gsx$name.$t; 
       var price = data.feed.entry[i].gsx$price.$t; 
       var notcompatiblewith = data.feed.entry[i].gsx$notcompatiblewithkommagescheiden.$t; 
       var img = data.feed.entry[i].gsx$img.$t; 
       var comp = notcompatiblewith.split(",") 

       $rootScope.parts.push({"id": parseInt(id), "name": name, "price": parseInt(price), "comp": comp, "img": img, "canAdd": true, "added": false}); 
      }; 
     }); 
    }; 

SSLはに等しい: 'https://spreadsheets.google.com/feeds/list/1l4XTKaLZihj5WDRuZJE5Y7RQ7Cr7SD_-tH0ctwhizWc/od6/public/values?alt=json'

HTML:以下

<div class="someName" ng-init="pl.partsInit()"> 
<ul class="plist"> 
     <li ng-repeat="part in pl.parts | orderBy:'+price'" ng-hide="part.added || !part.canAdd" class="plLi"> 
      <div class="plistDivName"> 
       {{part.name}} <!--alternatief: --> 
      </div> 
      <div> 
       <img src="{{part.img}}" class="listIMG"></img> 
      </div> 
      <div> 
       {{part.price | currency : "&euro;"}} <!-- | currency om 2 cijfers na de komma te krijgen --> 
      </div> 
      <div class="plistDivButton"> 
       <button ng-click="pl.ap = pl.ap + 1; pl.jadd(part.id); pl.stillPossible(part.id)" ng-show="part.canAdd" class="addButton">+</button> <!-- veranderd value van pl.parts.added naar true --> 
      </div> 
     </li> 
    </ul> 
</div> 
+0

あなたは 'NG-IF ="準備を追加することができます。私は、角アプリにバックエンドから値を渡したいとき、私はNG-INITを使用

// register controller in html <div data-ng-controller="myCtrl" data-ng-init="init()"></div> // in controller $scope.init = function() { // ---------- Your Code ---------- }; 

:実装はかなりのようになります"' 'ng-init'の隣にあり、初期化が完了した後にのみ' $ scope.ready'を設定します – floribon

+1

ng-initの中の呼び出しがタイムアウトで遅れるのは非常に悪い習慣です(ユーザの接続が遅い?)。あなたの質問を更新して、スプレッドシートを呼び出す方法とコントローラを起動する方法を示すことができますか? –

+0

$ httpサービスを使用してデータをその配列にロードしていますか?そのデータはどこから来ていますか? –

答えて

0

あなたは、必要に応じて変更することができたことで、テンプレートのコードです。

// at the bottom of your controller 
var init = function() { 
    // ---------- Your Code ---------- 

}; 
// and fire it after definition 
init(); 

また、ng-initディレクティブを参照してください。

<div data-ng-controller="myCtrl" data-ng-init="init('%some_backend_value%')"></div> 
関連する問題