Kaker
Kaker

Reputation: 645

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

<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

Answers (2)

Roh&#236;t J&#237;ndal
Roh&#236;t J&#237;ndal

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

Christian Esperar
Christian Esperar

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

Related Questions