2017-08-01 30 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(); 
     } 
    }); 

}])