Javascript 从事件调用时,$http call在指令中不起作用

Javascript 从事件调用时,$http call在指令中不起作用,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我遇到了一个奇怪的行为发生在一个角度的指令,我一点也不明白;让我用代码解释一下,我认为这是最简单的方法 什么是有效的: .directive('myDirective', function ($http) { return { template: '...', link: function (scope, elem, attrs) { function getData() { console.log(

我遇到了一个奇怪的行为发生在一个角度的指令,我一点也不明白;让我用代码解释一下,我认为这是最简单的方法

什么是有效的:

.directive('myDirective', function ($http) {
    return {
        template: '...',
        link: function (scope, elem, attrs) {
            function getData() {
                console.log('Starting...');
                $http.get(...).success(/* update the template */);
            }
            getData();
        }
    };
});
不包括:

.directive('myDirective', function ($http) {
    return {
        template: '...',
        link: function (scope, elem, attrs) {
            function getData() {
                console.log('Starting...');
                $http.get(...).success(/* update the template */);
            }
            scope.$on('callMyDirective', getData);
        }
    };
});
在第二个示例中,$http调用被完全忽略,甚至没有在承诺的finally()部分打印内容(实际上没有网络活动),而日志在这两种情况下都被正确打印(当然,在第二种情况下,在触发事件后,通过单击按钮)

有线索吗

非常感谢

更新:

我找到了错误的根源,但这又是另一个问题。问题是我有一个拦截器,在那里我包装了所有的请求,但是这个请求应该用一个承诺对用户进行身份验证,我只在身份验证结束后解析它。我的拦截器是这样的:

request: function (config) {
    if (userHasArrived || isAuthUrl) {
        return config || $q.when(config);
    }

    var defer = $q.defer();

    $rootScope.$on('user_logged_in', function (user) {
        console.log('User logged in');
        userHasArrived = true;
        defer.resolve(config);
    });
    return defer.promise;
}
同时,我有一个运行auth请求的服务,并在成功时广播上面显示的事件。这适用于所有其他请求,但对于我上面提到的指令,它位于不同的“页面”,并且从未在拦截器中接收到事件(日志从未打印)

只有当我对我的“auth”URL强制执行第二个请求时,才会接收到事件,并且指令stuff可以正常工作。服务是否会在拦截器之前加载?在这种情况下,有没有办法控制这个加载顺序

更新:

好的,很抱歉打扰你,但是如果有人仍然感兴趣,我找到了第二个问题的解决方案。这是事情发生的顺序:

  • 身份验证请求启动
  • 身份验证请求结束,广播成功
  • 指令请求启动(按需,单击按钮)
  • 验证成功开始被监听
  • 所以这件事是在没人听之前就被炒了。这将是最终代码(不完整的版本):

    我希望这能奏效

    .directive('myDirective',函数($http){
    返回{
    模板:“…”,
    链接:功能(范围、要素、属性){
    scope.getData=函数(){
    log('Starting…');
    $http.get(…).success(/*更新模板*/);
    }
    作用域:$on('callMyDirective',函数(事件,参数){
    scope.getData();
    });
    }
    };
    
    });
    您可能应该使用
    控制器
    属性而不是
    链接
    是否有任何错误?我认为这与事件
    callMyDirective
    的广播方式有关。它是从
    $rootScope
    中完成的吗。。。在这种情况下,我为什么要使用控制器功能?;我没有得到任何错误;是的,我正在$rootScope上播放这个活动。你能用plnkr或小提琴什么的重新播放吗
    $rootScope.$on('user_logged_in', function (user) {
        userHasArrived = true;
    });
    
    return {
        request: function (config) {
            if (userHasArrived || isAuthUrl) {
                return config || $q.when(config);
            }
    
            var defer = $q.defer();
    
            var $off = $rootScope.$on('user_logged_in', function (user) {
                userHasArrived = true;
                $off(); // You better unsubscribe for the events too, if you do something along this lines
                defer.resolve(config);
            });
            return defer.promise;
        }
    };