如何使用ng-click從數(shù)組中刪除項(xiàng)目或?qū)ο??我正在嘗試編寫一個(gè)函數(shù),使我能夠在單擊按鈕時(shí)刪除項(xiàng)目,但我認(rèn)為我對(duì)該函數(shù)感到困惑 - 我使用了$digest嗎?HTML和app.js:<ul ng-repeat="bday in bdays">
<li>
<span ng-hide="editing" ng-click="editing = true">{{bday.name}} | {{bday.date}}</span>
<form ng-show="editing" ng-submit="editing = false">
<label>Name:</label>
<input type="text" ng-model="bday.name" placeholder="Name" ng-required/>
<label>Date:</label>
<input type="date" ng-model="bday.date" placeholder="Date" ng-required/>
<br/>
<button class="btn" type="submit">Save</button>
<a class="btn" ng-click="remove()">Delete</a>
</form>
</li></ul>$scope.remove = function(){
$scope.newBirthday = $scope.$digest();
};
3 回答

至尊寶的傳說(shuō)
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
這是一個(gè)正確的答案:
<a class="btn" ng-click="remove($index)">Delete</a>$scope.remove=function($index){ $scope.bdays.splice($index,1); }
我認(rèn)為這是錯(cuò)誤的,因?yàn)槟銈鬟f$index
了參數(shù),但你在控制器中使用了愿望。如我錯(cuò)了請(qǐng)糾正我 :)