2014-10-27 69 views
0

我想刪除我的表中的空行。我必須將它設置爲我的控制器中的空對象,或者我得到一個錯誤消息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: ''} 
    } 

回答

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>

+0

你能告訴我怎麼它適用於本http://plnkr.co/edit/wtxQT4cJ1oFYkMHw0ji4?p=preview – texas697 2014-10-27 15:26:46

+0

@ user3919120請參閱http://plnkr.co/edit/n8sEWMLuxvZkVGBhjl6h?p=preview – sylwester 2014-10-27 15:31:51