angularjs - Angular Delay custom directive's link function -
i have simple custom directive checks , sets href
on element. need check href data loaded server (async) make sure user has access link(kind of acl).
so how can delay link function of doing job until data has finished loading?
ok, cook self, appreciate pointers improving it, made gist
angular.module('mymodule') .factory('dataloader', ['$http', function ($http) { var __list = null; var __request = null; return function (callbak) { if (__list) callbak(__list); else { if (!__request) __request = $http({ method: 'get', url: '/myurl', params: {} }); __request.success(function (d) { if (d.data) { __list = d.data; callbak(__list); } }); } } }]) .directive('mydirective', ['dataloader', function (dataloader) { return { restrict: "a", compile: function (scope, element, attrs) { return function (scope, element, attrs) { dataloader(function (acl) { console.log(acl.length); var href = attrs.mydirective; if (href) { if (href[0] != '#') href = '#' + href; element.attr('href', href); } }); }; } }; }]);
Comments
Post a Comment