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?

Was it helpful?

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

OTHER TIPS

There are two things you can use

  • ng-cloak

  • ng-show and ng-hide and custom scope property

Also consider using animation instead of creating manual animation within directive

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top