角度JS表格计算

时间:2016-02-06 16:58:04

标签: html angularjs

我是angular.js的新手,我想计算表单字段,我能够计算$ scope.piece并将结果输出到html。问题是我无法计算$ scope。$ scope.typeofwritingings。如何获取$ scope.typeofwritingings的值。比如$ scope.calculation =($ scope.piece * $ scope.typeofwritingings);

(function(angular) {
  'use strict';
angular.module('formExample', ['ngMessages'])
  .controller('ExampleController', ['$scope', function($scope) {

$scope.typeofwritings = [{
			value: '5',
			text: 'Writing from scratch'
		  }, {
			value: '3',
			text: 'Editing or Proofreading'
		  }
		  ];
		  
$scope.pieces = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
$scope.piece = 1;

		$scope.calculate = function(){
		$scope.calculation = $scope.piece;
		
		}
		$scope.calculate();
		  
	
  }]);
})(window.angular);
<tr>
      <td width="186"><label for="type_writing">Type of Writing:</label></td>
      <td width="269"><select ng-change="calculate()" ng-model="type_writing" ng-options="o.value as o.text for o in typeofwritings" ng-init="type_writing='5'" name="type_writing" id="type_writing">
      </select>
      </td>
</tr>

<tr>
      <td><label for="length">Article Length:</label></td>
      <td><select ng-model="length" ng-options="o.value as o.text for o in lengths" ng-init="length='1'" name="length" id="length">
      </select></td>
</tr>

<tr>     
      <td><strong>Order Cost</strong><div style="color:#D76D25; font-size:24px;">${{calculation}}</div></td>
    </tr>
<tr>

1 个答案:

答案 0 :(得分:0)

要获取对象数组中的值,可以执行以下操作:

scope.calculation = ($scope.piece * $scope.typeofwritings[0].value); // 1*5
scope.calculation = ($scope.piece * $scope.typeofwritings[1].value);// 1*3