$rootScope.getPricesData = function (typ) {
var data = {
type: typ
}
$http({
url: '...',
method: 'POST',
data: data
}).then(function (res) {
$rootScope.pricesData = res.data;
console.log(res);
})
}
它的效果很好,html
<div ng-init="getPricesData('template')">info template</div>
<div ng-init="getPricesData('subscription_month')">info subscription_month</div>
我在控制台中看到这两个 div 关于订阅(不是模板和订阅)的信息,我正确地看到了这个数据,并且该函数被调用了两次(对于模板和订阅)。
我认为范围覆盖(但我不知道如何解决) 请帮忙
最佳答案
您可以使用 Javascript ternary operator处理场景。
$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; }) }
您可以创建一个空对象,然后在成功响应时将响应数据分配到对象的相应属性中。
$scope.resData = {}; $rootScope.getPricesData = function (typ) { var data = { type: typ } $http({ url: '...', method: 'POST', data: data }).then(function (res) { $scope.resData[typ] = res.data; }) }
关于javascript - 如何使具有不同数据的两个 div 中的相同范围可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42279205/