Angularjs $scope在我切换页面时不更新
我正在处理一个从模板A到模板B再到模板A的应用程序。在模板A上,用户单击按钮进入模板B。在模板B上,用户添加金额,然后点击提交。然后程序返回模板A并显示在模板B中输入的内容,但提交的编号在范围中未更新,显示为空。由于某种原因,当我从模板B启动应用程序到模板A时,范围会更新 我在用一家工厂Angularjs $scope在我切换页面时不更新,angularjs,ionic-framework,scope,Angularjs,Ionic Framework,Scope,我正在处理一个从模板A到模板B再到模板A的应用程序。在模板A上,用户单击按钮进入模板B。在模板B上,用户添加金额,然后点击提交。然后程序返回模板A并显示在模板B中输入的内容,但提交的编号在范围中未更新,显示为空。由于某种原因,当我从模板B启动应用程序到模板A时,范围会更新 我在用一家工厂 .factory('myService', function(){ var budget = { limit: null }; function set(data){ budget
.factory('myService', function(){
var budget = {
limit: null
};
function set(data){
budget.limit = data;
}
function get(){
return budget.limit;
}
return{
set: set,
get: get,
print: console.log(budget.limit)
}
})
这是我为名为BudgetCalc的模板A编写的代码
<ion-view view-title="BudgetCalc">
<ion-content class="padding">
<a href="#/tab/dash/addBudget"><button>Start Budgeting</button></a>
<h2>Your Limit is {{limit}}</h2>
</ion-content>
</ion-view>
您可以使用localstorage在控制器之间共享数据
.controller('SetLimitCtrl', function($scope, $localstorage){
$scope.setLimit = function(limit){
if (limit != null) {
$localstorage.set("limit",limit);
console.log($localstorage.get("limit"));
}
}
})
.controller('BudgetCtrl', function($scope, $localstorage) {
$scope.limit = $localstorage.get("limit");
console.log("This is your limit " + $scope.limit);
//// Dont forgot to clear limit when you complete the further process
})
工厂地址:
.factory('$localstorage', ['$window', function ($window) {
return {
set: function (key, value) {
$window.localStorage[key] = value;
},
get: function (key, defaultValue) {
return $window.localStorage[key] || defaultValue;
},
setObject: function (key, value) {
$window.localStorage[key] = JSON.stringify(value);
},
getObject: function (key, nulled) {
nulled = '[]';
try {
return JSON.parse($window.localStorage[key] || '[]');
} catch (e) {
}
},
delete: function (key) {
$window.localStorage.removeItem(key);
},
};
}])
每次返回视图A时,BudgetCtrl中的console.log是否正在运行?否,它仅第一次运行。返回时,它不会再次显示。返回以查看时,请添加“重新加载”选项$state.go('viewA'),{},{reload:true})可能是我完全错了,但是如果OP想要访问两个控制器中的数据,他不应该使用服务模块而不是工厂模块。服务是单例的,而工厂每次都被实例化。。。
.controller('SetLimitCtrl', function($scope, $localstorage){
$scope.setLimit = function(limit){
if (limit != null) {
$localstorage.set("limit",limit);
console.log($localstorage.get("limit"));
}
}
})
.controller('BudgetCtrl', function($scope, $localstorage) {
$scope.limit = $localstorage.get("limit");
console.log("This is your limit " + $scope.limit);
//// Dont forgot to clear limit when you complete the further process
})
.factory('$localstorage', ['$window', function ($window) {
return {
set: function (key, value) {
$window.localStorage[key] = value;
},
get: function (key, defaultValue) {
return $window.localStorage[key] || defaultValue;
},
setObject: function (key, value) {
$window.localStorage[key] = JSON.stringify(value);
},
getObject: function (key, nulled) {
nulled = '[]';
try {
return JSON.parse($window.localStorage[key] || '[]');
} catch (e) {
}
},
delete: function (key) {
$window.localStorage.removeItem(key);
},
};
}])