0
我嘗試使用輸入列表管理數組。
我希望用戶只有在最後一個不爲空時才能添加項目。我不明白爲什麼我不能綁定數組的更改。
我在html中完全使用了相同類型的代碼,但效果並不理想。
我在哪裏錯了?
這裏是一個plunker:http://plnkr.co/edit/IpivgS5TWNQFFxQlRloa?p=preview
HTML
<div class="row" ng-repeat="radio in radioList track by $index">
<p class="input-group">
<input type="text" class="form-control" ng-model="radio.code" placeholder="enter sthg" ng-required="true" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="removeRadioList($index);"><i class="glyphicon glyphicon-remove">X</i>
</button>
</span>
</p>
</div>
</div>
<div class="col-xs-3">
<button class="btn btn-primary" ng-click="addRadioList();" ng-disabled="disableAddRadio">Ajouter un cliché</button>
{{radioList[radioList.length-1] }}
</div>
** ** JS
$scope.radioList = [{
code: ''
}];
$scope.addRadioList = function() {
$scope.radioList.push({
'code': ''
});
}
$scope.removeRadioList = function(i) {
$scope.radioList.splice(i, 1);
}
$scope.$watch('radioList', function(newValue, oldValue) {
console.log('change');
$scope.disableAddRadio = (angular.isDefined($scope.radioList[$scope.radioList.length - 1].code) || $scope.radioList.length < 1);
});