2017-08-01 61 views
0

我在下面有一段代码。如何将承诺值传递给函数

我需要传递一个值,我从它的代码

这里说**value**承诺得到低于

Auth.loggedInUser().then(function (user){ 
    return user.id 
}); 

下面我的承诺是角码

.run(['$rootScope', 'ngCart','ngCartItem', 'stores', function ($rootScope, ngCart, ngCartItem, stores) { 

    $rootScope.$on('ngShoppingCart:changed', function(){ 
     ngCart.$save(); 
    }); 

    if (angular.isObject(stores.get(**value**))) { 
     ngCart.$restore(stores.get(**value**)); 

    } else { 
     ngCart.init(); 
    } 

}]) 

如何我可以将异步函数中返回的值(值)传递给的角码中吗?不确定适当的方法。

+0

内得到它下面的作品,但对于这样的事情。这个空的= function(){ $ rootScope。$ broadcast('ngShoppingCart:change',{}); this。$ cart.items = []; Auth.loggedInUser(),然后(功能(用户){ $ window.localStorage.removeTheItem(user.id); })。 };' 我得到错误,如'TyppeError:无法读取属性'项'的未定义' –

回答

2

也许这将工作

.run(['$rootScope', 'ngCart','ngCartItem', 'stores', function ($rootScope, ngCart, ngCartItem, stores) { 

    $rootScope.$on('ngShoppingCart:changed', function(){ 
     ngCart.$save(); 
    }); 
    Auth.loggedInUser().then(function (user){ 
     if (angular.isObject(stores.get(user.id))) { 
      ngCart.$restore(stores.get(user.id)); 

     } else { 
      ngCart.init(); 
     } 
    }); 

}]) 
+0

明白它的工作原理!但我有另一个不起作用,我在上面的工作 –

+0

上面额外张贴,但是这样的事情呢。 'this.empty = function(){$ rootScope。$ broadcast('ngShoppingCart:change',{});这个。$ cart.items = []; Auth.loggedInUser()。then(function(user){$ window.localStorage.removeTheItem(user.id);}); };'我得到的错误,例如'TyppeError:无法读取未定义的属性'项目' –

+0

代码在评论 –

0

注入身份验证服务运行和使用正则表达式的承诺

.run(['$rootScope', 'ngCart', 'ngCartItem', 'stores', 'Auth', function($rootScope, ngCart, ngCartItem, stores, Auth) { 

    $rootScope.$on('ngShoppingCart:changed', function() { 
     ngCart.$save(); 
    }); 

    Auth.loggedInUser().then(function(user) { 

     if (angular.isObject(stores.get(user.id))) { 
      ngCart.$restore(stores.get(user.id)); 

     } else { 
      ngCart.init(); 
     } 
    }); 

}])