如何使用Angular的移动jQuery Asychronous API

时间:2017-01-05 15:02:00

标签: angularjs angular-promise

我想为此登录函数添加角度标记,以将代码从移动Jquery转移到Angularjs。有什么建议吗?

this.login = function(login, password, cb){
    this.execute('user/login', {email: login, password: password}, cb);
};

this.logout = function(cb){
    this.execute('user/logout', {}, cb);
}

this.userGet = function(email, cb, fcb){
    this.execute('user/get_user', {email: email}, cb, fcb);
}

// code for the login for a web application

1 个答案:

答案 0 :(得分:0)

将使用回调的异步API转换为Angular承诺:

/*
this.login = function(login, password, cb){
    this.execute('user/login', {email: login, password: password}, cb);
};
*/

function loginPromise(login, password) {
    var future = $q.defer();
    this.execute('user/login',
                 {email: login, password: password},
                 future.resolve
    );
    return future.promise;
}

通过从异步API创建$ q服务承诺,对范围的任何更改都将与AngularJS框架摘要周期集成。

有关详细信息,请参阅AngularJS $q Service API Reference