我们在下面的代码基本上有ng-change触发changeValue()重新计算每次尝试1到尝试9的总和。
但在 TypeError上有以下错误:无法读取未定义的属性'attempt1'。我相信这与子/父$范围可访问有关......但不知道如何解决这个问题。
BTW ......我们正在使用AngularJS和Ionic Framework。
.controller('RoutineActivityEditController', ['$scope', '$http', '$state', '$stateParams', '$localStorage', '$ionicPopup', function($scope, $http, $state, $stateParams, $localStorage, $ionicPopup) {
$scope.apiurl = $localStorage.apiurl;
$scope.options = [ {label: "Not Ok", value: 0},
{label: "ok", value: 1}
];
$http.get($scope.apiurl + '/routineactivity/' + $stateParams.uuId).success(function(data) {
$scope.routineActivityData = data;
$scope.total = $scope.routineActivityData.total;
$scope.target = $scope.routineActivityData.target;
// Debug JSON
//console.log('TEST2:' + data);
//$scope.showValues = JSON.stringify(data);
//console.log('TEST3:' + $scope.showValues);
$scope.changeValue = function ($scope)
{
$scope.total = $scope.routineActivity.attempt1.value;
//+ $scope.routineActivity.attempt2 +
// $scope.routineActivity.attempt3 + $scope.routineActivity.attempt4 +
// $scope.routineActivity.attempt5 + $scope.routineActivity.attempt6 +
// $scope.routineActivity.attempt7 + $scope.routineActivity.attempt8 +
// $scope.routineActivity.attempt9;
}
});
<form name="routineActivityForm" ng-submit="sendData()">
<div ng-if="routineActivityData.item.routine.maxnoofattempt == 10">
<div class="item item-input">Total: {{ total }} </div>
<div class="item item-input">Target: {{ target }}</div>
<label class="item item-input item-select">
<div class="input-label">Attempt #1</div>
<select name="attempt1" ng-model="routineActivity.attempt1" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt1=routineActivityData.attempt1">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #2</div>
<select name="attempt2" ng-model="routineActivity.attempt2" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt2=routineActivityData.attempt2">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #3</div>
<select name="attempt3" ng-model="routineActivity.attempt3" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt3=routineActivityData.attempt3">
</select>
<!--<select name="attempt3" ng-model="routineActivity.attempt3" ng-change="changeValue()">
<option ng-repeat="v in [0,1]" value="{{v}}" ng-selected="v==routineActivityData.attempt3">{{v}}</option>
</select>-->
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #4</div>
<select name="attempt4" ng-model="routineActivity.attempt4" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt4=routineActivityData.attempt4">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #5</div>
<select name="attempt5" ng-model="routineActivity.attempt5" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt5=routineActivityData.attempt5">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #6</div>
<select name="attempt6" ng-model="routineActivity.attempt6" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt6=routineActivityData.attempt6">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #7</div>
<select name="attempt7" ng-model="routineActivity.attempt7" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt7=routineActivityData.attempt7">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #8</div>
<select name="attempt8" ng-model="routineActivity.attempt8" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt8=routineActivityData.attempt8">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #9</div>
<select name="attempt9" ng-model="routineActivity.attempt9" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt9=routineActivityData.attempt9">
</select>
</label>
</div>
<button class="button button-full button-positive">Save</button>
<label >RESPONSE: {{ PostDataResponse }}</label>
</div>
</form>