如何在change事件中读取对象的属性

时间:2015-01-02 19:32:06

标签: angularjs

这是我的第一个有角度的应用......我必须说我很惊讶框架是多么棒。任务是建立一个价格计算器,在选择州后应用州税。这就是我在JS中的一些东西

function testApp($scope){
    $scope.quantity = 1;
    $scope.price = 10.99;

    $scope.states = [
        {stateName:'--'},
        {stateName:'New York',stateId: 'NY', tax: 0.5},
        {stateName:'Pennsylvania', stateId: 'PA', tax: 0.3},
        {stateName:'California', stateId: 'CA', tax: 0.6}
    ];
}

我的标记看起来像这样

<div ng-app="" ng-controller="testApp">
Quantity: <input type="number" max="5" ng-model="quantity"/>
Price: <span class="price">{{price|currency}}</span>

State: 
<select>
  <option ng-repeat="x in states" id="{{x.stateId}}">
    {{x.stateName}}
  </option>
</select>

    <h2 id="greeting">Total: {{quantity*price|currency}}</h2>
</div>

我无法理解如何读取状态对象和更改的tax属性并将其传递给总计。也许很简单,我无法从概念上理解

这是我的小提琴

http://jsfiddle.net/sghoush1/ssuybyzc/2/

1 个答案:

答案 0 :(得分:2)

查看data-ng-options。你不应该建立这样的选择。这是更新的html:

<select data-ng-model="selectedState" data-ng-options="state as state.stateName for state in states">

随着您的更新小提琴:http://jsfiddle.net/dLb9kx6q/