我正在尝试删除表格中的空行。我必须将它设置为我的控制器中的空对象,否则我会收到错误消息Cannot read property 'push' of undefined. Note that I do not get this error in my plunkr when I comment out the empty object in the controller. not sure why i dont need it in the plunkr but I need it in my project.
[plunkr][1]
$scope.question = {
options: [{}],
option: { number: ''}
};
$scope.addOption = function() {
$scope.question.options.push($scope.question.option);
//clear out the option
$scope.question.option = {number: '', description: ''}
}
答案 0 :(得分:1)
只需使用:
$scope.question = {
options: [],
option: {
number: ''
}
};
var app = angular.module('app', []);
app.controller('fCtrl', function($scope) {
$scope.question = {
options: [],
option: {
number: ''
}
};
$scope.addOption = function() {
$scope.question.options.push($scope.question.option);
//clear out the option
$scope.question.option = {
number: '',
description: ''
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="fCtrl">
<table>
<tr ng-repeat="question in question.options">
<td>{{$index}}</td>
<td>{{question}}</td>
</tr>
<table>
<button ng-click="addOption()">Add</button>
</div>
</div>