资源查询调用执行时机

resource query call execution timing

我的代码如下:

    // Find a list of Adverts
    $scope.find = function() {
        debugger;
        $scope.adverts = Adverts.query();
        doSomethingWithAdverts();
    };

其中 $scope.find()data-ng-init="find()"

调用

当我通过函数调试时,当我跨过 $scope.adverts = Adverts.query(); 行并将调试器保持在 doSomethingWithAdverts(); 上时,我没有看到 firebug 中的 HTTP GET 调用,只有当我恢复执行并退出这个功能时。为什么会这样,我以为 Adverts.query(); 会启动 ajax 调用?

这会在尝试使用 doSomethingWithAdverts()

中的响应数据时导致不正确的行为

我找到了答案:

资源查询返回响应后需要执行的任何内容都放在回调函数中,如下所示:

    // Find a list of Adverts
    $scope.find = function() {
        debugger;
        $scope.adverts = Adverts.query(function(){
           doSomethingWithAdverts();
        });

    };