2014-05-05 61 views
0

開始學習angularjs我正在嘗試一些簡單的例子。 我想在我看來2個變量total1和total2,它們是在我的js函數中計算的。Angularjs如何返回多個變量

我的js

var total1 = 0; 
    var total2 = 0; 

    // Use the angular forEach helper method to 
    // loop through the services array: 

    angular.forEach($scope.services, function(s){ 
     if (s.active){ 
      total1+= s.price; 
      total2+= 1; 
     } 
    }); 

    return [total1, total2]; 
}; 

我的HTML

 <div class="total"> 

      <!-- Calculate the total price of all chosen services. Format it as currency. --> 
      Totaal: <span>{{total1 | currency:"€ "}}</span> 
      Selected: <span>{{total2 | currency:"€ "}}</span> 
     </div> 

有沒有辦法來顯示視圖withing角變量,使用somehing像{{my_var1}} {{my_var2}}

+0

爲什麼你需要返回這些變量?將這些變量附加到$ scope,您可以在視圖中訪問它 – BKM

+0

用$ scope.total1 = 0替換var total1 = 0,並執行$ scope.total1 + = s.price。與total2一樣。 –

+0

謝謝你的回答。我明白了(有點)現在的工作http://jsfiddle.net/richard7/BLJ5M/ – Richard

回答

1

您只需要將您的變量作爲您的範圍中的字段公開。

$scope.total1 = 0; 
$scope.total2 = 0; 

這應該就夠了,不需要返回任何東西。