Directiva de habilitación de AngularJS una vez que finaliza la obtención de $http
-
21-12-2019 - |
Pregunta
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
});
}
};
});
Tengo el AngularJS anterior y cuando ejecuto mi página se muestra Console.Log [cycle] terminating; too few slides: 0
y luego muestra el Get Request
después.
¿Cómo podría ejecutar la directiva una vez que $http.get
ha terminado?
Solución
intenta hacer esto
<div cycle ng-if="drinks"></div>
De esa manera, el elemento que contiene la directiva se compilará en el navegador solo cuando las bebidas estén disponibles.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow