2017-02-16 3 views
0
$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異なるデータを持つ2つのdivで同じスコープを使用するにはどうすればよいですか?

<div ng-init="getPricesData('template')">info template</div> 
<div ng-init="getPricesData('subscription_month')">info subscription_month</div> 

と私は私が正しく、このデータを見て、関数がのために(2回呼ばれたコンソール(tempateおよびサブスクリプションではない)サブスクリプションに関するこれら二つのdivの情報で見ますテンプレートとサブスクリプション)。

ヘルプ

+0

あなたは、ルータのコントローラを宣言しても、コントローラにそれが二回呼び出される場合... – Gopinath

+1

が今までrootscopeを使用してみてください決してそれらのいずれかが代わりにあなたが利用することができます悪い習慣 –

+0

thatsの削除ディレクティブ –

答えて

1
  • あなたはシナリオを処理するためのJavascript ternary operatorを使用することができるようにすることができオブジェクトあなたの$ rootScopeを作るです。

    $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; 
        }) 
    } 
    
  • あなたは1つの空のオブジェクトを作成し、成功応答にオブジェクトのそれぞれの特性に応じてデータを割り当てることができます。

    $scope.resData = {}; 
    $rootScope.getPricesData = function (typ) { 
        var data = { 
         type: typ 
        } 
        $http({ 
         url: '...', 
         method: 'POST', 
         data: data 
        }).then(function (res) { 
         $scope.resData[typ] = res.data; 
        }) 
    } 
    
+0

ありがとう、 – Kaker

0

のために、私は、これは常に上書きされますとお考えください 私はスコープのオーバーライドを考える(私はそれを修正する方法がわかりません)。一つの可能​​な解決策は、それが$ rootScope.pricesData [標準]

関連する問題