Reputation: 645
$rootScope.getPricesData = function (typ) {
var data = {
type: typ
}
$http({
url: '...',
method: 'POST',
data: data
}).then(function (res) {
$rootScope.pricesData = res.data;
console.log(res);
})
}
And its works good and html
<div ng-init="getPricesData('template')">info template</div>
<div ng-init="getPricesData('subscription_month')">info subscription_month</div>
and I see in these two div's information about subscription(not tempate and subscription) in Console i see this data correctly, and the function was called twice (for template and subscription).
I think the scope overrides (but I do not know how to fix that) Please for help
Upvotes: 0
Views: 115
Reputation: 27242
You can use Javascript ternary operator to handle the scenario .
$rootScope.getPricesData = function (typ) {
var data = {
type: typ
}
$http({
url: '...',
method: 'POST',
data: data
}).then(function (res) {
if(typ == 'template') ? $scope.templatePricesData = res.data : $scope.subscriptionPricesData = res.data;
})
}
You can create one empty object and then on success response assign the response data into the respective properties of an object.
$scope.resData = {};
$rootScope.getPricesData = function (typ) {
var data = {
type: typ
}
$http({
url: '...',
method: 'POST',
data: data
}).then(function (res) {
$scope.resData[typ] = res.data;
})
}
Upvotes: 1
Reputation: 528
I think this will always override. One possible solution is make your $rootScope an object so it can be like $rootScope.pricesData[typ]
Upvotes: 0