Answer the question
In order to leave comments, you need to log in
How to delay building a document or processing a directive until data is received from the server?
js file:
'use strict';
angular.module('myApp')
.factory('Order', ['$resource',
function ($resource) {
return $resource('/api/orders/:id', { id: 'all' } );
}
])
.controller('orderCtrl', ['$scope', '$routeParams', 'Order',
function ($scope, $routeParams, Order) {
// Если здесь не заполнить скоп то в обработке директивы будет ошибка
$scope.orders = [{
name: 'name'
}];
console.log('controller');
Order.query({id: 'all'}, function(orders) {
$scope.orders = orders;
console.log('get data from server');
});
}
])
.directive('orderDrct', ['$compile',
function ($compile) {
return {
restrict: 'A',
link: function (scope, element) {
var html = '';
angular.forEach(scope.orders, function (ord) {
html += '<h4>' + ord.name + '</h4>';
});
element.append($compile(html)(scope));
console.log('end of directive');
}
};
}
]);
controller
end of directive
get data from server
div.marketing
h3 It made with ng-repeat
div.order(ng-repeat='order in orders')
h4 {{order.name}}
div.marketing
h3 It made with derective
div(order-drct)
Answer the question
In order to leave comments, you need to log in
I used $broadcast for such a task. In your case, add a 'dataloaded' message to the controller:
Order.query({id: 'all'}, function(orders) {
$scope.orders = orders;
console.log('get data from server');
$scope.$broadcast('dataloaded');
});
.directive('orderDrct', ['$compile',
function ($compile) {
return {
restrict: 'A',
link: function (scope, element) {
scope.$on('dataloaded', function ()
{
// Ваш код
console.log('end of directive');
});
}
};
}
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question