2015-06-01 27 views
0

我有简单的自定义指令,检查并设置其元素href。 但我需要检查与从服务器(异步)加载的数据的href,以确保用户有权访问该链接(ACL的种类)。角延迟自定义指令的链接功能

那么我怎么能延迟链接功能的工作,直到这个数据完成加载?

+0

把它在$超时。 – user3227295

+0

@ user3227295如果你的意思是延迟$ timeout,这是一个非常糟糕的做法,如果你的意思是$ timeout没有延迟,$ tmeout不知道http请求及其状态,只适用于$ scope vars和消化 – Exlord

回答

0

好吧,我煮东西为我自己,感谢任何指针改进它, 我做了一个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); 
         } 
        }); 
       }; 
      } 
     }; 
    }]);