[英]How to make the same scope available in two div's with different data?
$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 和它的作品好,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). 我在控制台的两个div信息中看到有关订阅(不是模板和订阅)的信息,我可以正确看到此数据,并且该函数被调用了两次(用于模板和订阅)。
I think the scope overrides (but I do not know how to fix that) Please for help 我认为范围会覆盖(但我不知道如何解决)请寻求帮助
You can use Javascript ternary operator to handle the scenario . 您可以使用Javascript 三元运算符来处理场景。
$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; }) }
I think this will always override. 我认为这将永远覆盖。 One possible solution is make your $rootScope an object so it can be like $rootScope.pricesData[typ]
一种可能的解决方案是将$ rootScope设置为对象 ,使其类似于$ rootScope.pricesData [typ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.