2016-08-24 7 views
0

私は単純なangularjsアプリケーションを持っています。このアプリケーションではスライダを実行し、スライダ要素は$ http要求から来ています。ここで

は参照用のコードです:

<div ng-app="myapp"> 
<div ng-controller="gridChildController" class="brand_slider" > 
    <div class='slider'> 
     <div class="slide col-md-5ths col-sm-4 col-xs-12 text-center" ng-repeat="x in brands" ng-init="$last && finished()"> 
      <a href="{{x.link}}"><img class="img-responsive center-block" ng-src="{{x.image}}" title="{{x.title}}" alt="{{x.title}}"></a> 
     </div> 
    </div> 
</div> 
</div> 

私はこのコードを実行すると、私は$のhttpからデータを取得しますがng-:ここ

var mainApp = angular.module("myapp", []); 

mainApp.run(['$rootScope', '$http', 
    function($rootScope, $http) { 
$http.post('process.php?ajax_type=getChild').success(function(data) { 
      if (data.success) { 
       console.log(data.child); // data received... 
       $rootScope.categories = data.child; 
      } 
     }); 
}]); 

mainApp.controller('gridChildController', function($scope, $http, $rootScope) { 
console.log($rootScope.categories); // this is also null.... 
$scope.brands = $rootScope.categories; 
    $scope.finished = function(){ 
     jQuery('.brand_slider').iosSlider({ 
      desktopClickDrag: true, 
      snapToChildren: true, 
      infiniteSlider: false, 
      navNextSelector: '.brands-next', 
      navPrevSelector: '.brands-prev', 
      lastSlideOffset: 3, 
      onSlideChange: function (args) { 

      } 
     }); 
    }; 
}); 

は、テンプレートファイルのコードですリピートは動作せず、コントローラではデータがnullになります。

+0

利用解決しない:だから、($scope.brandsにコピーする必要は)categoriesにまっすぐにバインドできます。 –

+1

あなたは '$ rootScope'をあなたのコントローラに注入しておらず、あなたのビューでコントローラを初期化していません。 – Dieterg

+0

@Dieterg絶対に私はコードコピーの貼り付けに間違いを犯しました。ありがとう、私は編集コードを確認してください。 –

答えて

0

$rootScopeに何かを置くと、それも子スコープで利用できます。あなたは、角度のルーティングを使用している場合

ng-repeat="x in categories" 
関連する問題