AngularJS enable directive once $http get finishes
-
21-12-2019 - |
Question
app.controller("MainController", function($scope, $http){
$scope.items= [];
console.log('hi');
$http.get('../assets/data/data.json').then(function(response) {
$scope.drinks =response.data.drinks;
});
});
app.directive('cycle', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
$(element).cycle({
fx: 'fade',
timeout: 10
});
}
};
});
I have the above AngularJS and when I run my page the Console.Log shows [cycle] terminating; too few slides: 0
and then it shows the Get Request
after.
How would I be able to run the directive once the $http.get
has finished?
Solution
try doing this
<div cycle ng-if="drinks"></div>
that way element that contains directive will be compiled in browser only when the drinks are available
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow