简体   繁体   English

在angular.js中使用promise

[英]using promise with angular.js

i use angular.js in front side. 我在前端使用angular.js。

in my controller.js i defined an init() method that will be called in 在我的controller.js中,我定义了一个init()方法,该方法将在

init of my controller. 我的控制器的初始化。

Init method definition: 初始化方法定义:

var init = function () {
$scope.callTeamsService();
if ($scope.teams.length == 0){
....
}else{
...
}
.....

};

in $scope.callTeamsService i filled in $scope.teams variable. $ scope.callTeamsService中,我填写了$ scope.teams变量。

$scope.callTeamsService method definition: $ scope.callTeamsService方法定义:

$scope.callTeamsService = function(){
        NavService.getTeams(function (response) {
                $timeout(function () {
                    $scope.teams = response;                        
                    }
                }, 200);
            });
    };

In my service.js i defined a getTeams method as follow: 在我的service.js中,我定义了一个getTeams方法,如下所示:

service.getEquipes = function (callback) {
$http.get(urlBase+'users/' + $rootScope.globals.currentUser.loggedUser.idUser + '/teams')
                    .success(function (response) {
                        callback(response);
                    });
            };

My problem is when $scope.teams.length == 0 condition is reached the 我的问题是当$ scope.teams.length == 0条件达到

service.getEquipes method in my service.js is not yet called. 我的service.js中的service.getEquipes方法尚未调用。

How can i modify this code in order to finish the execution of $scope.callTeamsService method before reaching $scope.teams.length == 0 condition. 我如何修改此代码,以便在达到$ scope.teams.length == 0条件之前完成$ scope.callTeamsService方法的执行。

  service/factory

   service.getEquipes = function () {
        return $http.get(urlBase+'users/' + $rootScope.globals.currentUser.loggedUser.idUser + '/teams');

        };


 // controller 
    var promise = NavService.getTeams.then (
           function(data) {
             //assign to $scope or do logic
           },
           function(err){
               console.log(err)
           } 
      )

How can i modify this code in order to finish the execution of $scope.callTeamsService method before reaching $scope.teams.length == 0 condition. 我如何修改此代码,以便在达到$scope.teams.length == 0条件之前完成$scope.callTeamsService方法的执行。

That's the wrong way round - you need to wait with executing the $scope.teams.length == 0 condition until the $scope.callTeamsService method has finished. 这是错误的方法-您需要等待 $scope.teams.length == 0条件的执行,直到$scope.callTeamsService方法完成。

The classical method would be to give the $scope.callTeamsService method a callback parameter and call that in the timeout instead of $scope.teams = response; 经典方法是给$scope.callTeamsService方法一个回调参数,并在超时时调用该参数,而不是$scope.teams = response; . Then you can put your condition in the init function in the callback that you pass. 然后,您可以将条件放在传递的回调的init函数中。

However, you seem to want to use promises. 但是,您似乎想使用promises。 For that, all of your functions (that all are asynchronous) should return a promise: 为此,您的所有函数(都是异步的)应return一个promise:

service.getEquipes = function (callback) {
    return $http.get(urlBase+'users/' + $rootScope.globals.currentUser.loggedUser.idUser + '/teams');
}

(that was easy, $http already returns promises) (这很简单, $http已经返回了promise)

$scope.callTeamsService = function() {
    return NavService.getTeams().then(function(teams) {
        return $timeout(function() {
            return teams;
        }, 200);
    });
};

(and $timeout does as well - by invoking then and returning it from the callback you can chain them and get a new promise for both) (和$timeout一样好-通过调用then从回调中返回它,您可以将它们链接起来并为两者获得新的承诺)

function init() {
    return $scope.callTeamsService().then(function(teams) {
        $scope.teams = teams;
        if (teams.length == 0) {
            …
        } else {
            …
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM