javascript - AngularJs get data from $http before controller init -
i have simple angularjs application, in want run slider , slider elements come $http request.
here code reference:
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); // 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) { } }); }; });
here code of template file:
<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>
when run code, data $http ng-repeat doesn't work, , in controller data null.
if put on $rootscope
available on child scopes. can bind straight categories
(no need copy $scope.brands
):
ng-repeat="x in categories"
Comments
Post a Comment