Typescript async/await doesnt update AngularJS view

前端 未结 10 522
小鲜肉
小鲜肉 2020-12-01 09:17

I\'m using Typescript 2.1(developer version) to transpile async/await to ES5.

I\'ve noticed that after I change any property which is bound to view in my async funct

10条回答
  •  借酒劲吻你
    2020-12-01 09:54

    I have examined the code of angular-async-await and It seems like they are using $rootScope.$apply() to digest the expression after the async promise is resolved.

    This is not a good method. You can use AngularJS original $q and with a little trick, you can achieve the best performance.

    First, create a function ( e.g., factory, method)

    // inject $q ...
    const resolver=(asyncFunc)=>{
        const deferred = $q.defer();
        asyncFunc()
          .then(deferred.resolve)
          .catch(deferred.reject);
        return deferred.promise;
    }
    

    Now, you can use it in your for instance services.

    getUserInfo=()=>{
    
      return resolver(async()=>{
    
        const userInfo=await fetch(...);
        const userAddress= await fetch (...);
    
        return {userInfo,userAddress};
      });
    };
    

    This is as efficient as using AngularJS $q and with minimal code.

提交回复
热议问题