Last active
August 29, 2015 14:22
-
-
Save Exlord/11cfe71253673752c19b to your computer and use it in GitHub Desktop.
Delay in directive's link fn until a http request is over in Angular 1.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
}; | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment