如何一个控制器值更改为另一个控制
问题描述:
var getAgentBalanceAmount = function (ClientId, UserId) {
$http({
method: 'GET',
url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
})
.then(function success(response) {
$scope.getBalAmount = response.data[0].BalanceAmount;
}, function error(response) {
alert('Error');
});
};
在这种方法中$ scope.getBalAmount有一定的价值,并装订成的index.html page.here索引页充当主page.i想改变其他这个值页面(在内容页面中)是否可能。提前感谢。如何一个控制器值更改为另一个控制
答
设置在Rootcope中。
var getAgentBalanceAmount = function (ClientId, UserId) {
$http({
method: 'GET',
url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
})
.then(function success(response) {
$rootscope.getBalAmount = response.data[0].BalanceAmount;
}, function error(response) {
alert('Error');
});
};
答
使用工厂。如果你想使用工厂,注入工厂(UserService)在控制器。
为了在index.html中获取金额值,在相应的控制器中注入工厂并使用get方法。
$scope.amount = UserService.getBalAmount();
JS
var getAgentBalanceAmount = function (ClientId, UserId) {
$http({
method: 'GET',
url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId;
})
.then(function success(response) {
UserService.setBalAmount(response.data[0].BalanceAmount);
}, function error(response) {
alert('Error');
});
};
Factory
app.factory("UserService", function() {
var balAmount;
return {
getBalAmount: function() {
return balAmount;
},
setBalAmount: function(amount) {
balAmount = amount;
}
};
答
坐落在rootscope或使用会话或本地存储方法。 :
var getAgentBalanceAmount = function (ClientId, UserId) {
$http({
method: 'GET',
url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
})
.then(function success(response) {
$rootscope.getBalAmount = response.data[0].BalanceAmount;
}, function error(response) {
alert('Error');
});
};
var getAgentBalanceAmount = function (ClientId, UserId) {
$http({
method: 'GET',
url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
})
.then(function success(response) {
$window.sessionStorage.token=response.data[0].BalanceAmount;
}, function error(response) {
alert('Error');
});
};
使用直接在会话存储与此代码获取另一页:在所有页 console.log($window.sessionStorage.token)
访问
最好的办法是提取'http.get()'代码放到'厂\役'并在不同的控制器中随时随地使用它。 – anoop