我有一个保存用户偏好值的用户偏好工厂。当页面加载时它是空的。用户登录后,将填写用户配置文件。伪代码
app.factory('pref', function($rootScope){
var pref = {}, age, name;
$rootScope.$on('logged.in', function(){
pref.name = 'sam';
pref.age = 30;
pref.currency = '$';
age = getAge(); name = getName();
})
function getName(){
//format name
return name;
}
function getAge(){
return age;
}
return {
currency: pref.currency,
age: age,
name: name
}
})
然后我在我的控制器中注入工厂:
app.controller('MainCtrl', function($scope, pref) {
$scope.name = pref.name; //Return undefined
var currency = pref.currency;
$scope.price = function(amount){
return amount + currency; //don't show $ sign
}
});
来自 pref 工厂的返回值未在控制器中更新。我如何使它工作?