【发布时间】:2017-07-26 14:50:06
【问题描述】:
我有一个简单的指令,可以在其元素/属性上嵌入一个 ng-if,以根据 $http 调用的响应隐藏它:
app.directive('showFor',
[
'authorizationService', function(authorizationService) {
return {
restrict: 'EA',
template: '<show-for-transclude ng-if="show"></show-for-transclude>',
transclude: true,
scope: { process: '@' },
link: function(scope, element, attrs, ctrl, transclude) {
var process = scope.process || attrs.process;
if (!_.isUndefined(attrs.showFor) && !_.isEmpty(attrs.showFor)) {
process = attrs.showFor;
authorizationService
.Authorize(process)
.then(function(result) {
scope.show = result;
});
}
}
};
}
]);
authorizationService 调用获取当前用户所属的组列表并将其缓存在会话存储中。这很好用,除非页面上有许多这样的指令,它会在第一个返回并缓存结果之前多次调用 $http 服务。
我的问题是,有没有办法告诉来自指令的多个实例的后续调用等待第一次调用的响应?
【问题讨论】:
标签: angularjs