Angularjs 如何将一个控制器值更改为另一个控制器值
在此方法中,$scope.getBalAmount有一些值并绑定到index.html页面。在这里,索引页面充当母版页。我想在其他页面(在内容页面)上更改此值。是否可能。请提前感谢。在rootscope中设置Angularjs 如何将一个控制器值更改为另一个控制器值,angularjs,service,factory,Angularjs,Service,Factory,在此方法中,$scope.getBalAmount有一些值并绑定到index.html页面。在这里,索引页面充当母版页。我想在其他页面(在内容页面)上更改此值。是否可能。请提前感谢。在rootscope中设置 var getAgentBalanceAmount = function (ClientId, UserId) { $http({ method: 'GET', url: $scope.ip + '/getAgentBalanceAmount?Cli
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');
});
};
使用工厂。若要使用工厂,请在控制器中注入工厂(UserService) 要在index.html中获取金额值,请将工厂注入适当的控制器并使用get方法
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');
});
};
JS
$scope.amount = UserService.getBalAmount();
工厂
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');
});
};
在rootscope中或使用会话或本地存储方法进行设置:
app.factory("UserService", function() {
var balAmount;
return {
getBalAmount: function() {
return balAmount;
},
setBalAmount: function(amount) {
balAmount = amount;
}
};
使用以下代码直接在会话存储中获取另一页:
console.log($window.sessionStorage.token)
访问所有页面最好的方法是将http.get()
代码提取到factory\service
中,并在不同的控制器中随时随地使用它。
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');
});
};