2016-08-24 68 views
0

我有一個簡單的angularjs應用程序,其中我想運行一個滑塊,滑塊元素來自$ http請求。

這裏是參考代碼:

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) { 

      } 
     }); 
    }; 
}); 

這裏是模板文件的代碼:

<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-重複不起作用,並且在控制器中我得到數據爲空。

+0

使用解析:這樣你就可以直接綁定到categories(不需要將它複製到$scope.brands)。 –

+1

您不是在控制器中注入'$ rootScope',並且您沒有在視圖中初始化控制器。 – Dieterg

+0

@Dieterg絕對我在代碼複製粘貼中犯了一些錯誤。謝謝,檢查它我有編輯代碼。 –

回答

0

如果你把東西放在$rootScope上,它也可以在子範圍內使用。如果你使用的角度路由

ng-repeat="x in categories" 
相關問題