简体   繁体   English

如何在两个div中使用不同的数据提供相同的作用域?

[英]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.

相关问题 如何将数据从同一服务加载到AngularJS中的两个不同的作用域变量? - How to load data from the same service to two different scope variable in AngularJS? Angular-两个相同的指令具有不同的数据-隔离范围 - Angular - two the same directives with different data - isolated scope 如何同时滚动两个div? - How to Scroll two div's at the same time? 如何在滚动时同时使两个具有不同高度的DIV到达页面底部? - How do I make 2 DIV's with different heights reach the bottom of the page at the same time on scroll? 我如何制作两个页面AngularJS共享相同的$ scope? - How can I make two pages AngularJS share the same $scope? Javascript / CSS 如何制作具有不同数据的多个 div 元素并输入到同一个输入表单中 - Javascript / CSS How to make several div elements with different data that are entered into the same input form 将.each数据写入具有相同div ID的两个不同的类 - Write .each data to two different classes with same div id 如何将相同的功能应用于同名的不同div? - How to apply same function to different div's of the same name? 如何将相同的jQuery添加到不同的div,但内容不同? - How to add same jquery to different div's but with different content? angular的控制器方法如何使$ scope可用于我的函数参数 - How does angular's controller method make $scope available to my function argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM