如何使用ng-click從數組中刪除項目或對象?我正在嘗試編寫一個函數,使我能夠在單擊按鈕時刪除項目,但我認為我對該函數感到困惑 - 我使用了$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 回答

至尊寶的傳說
TA貢獻1789條經驗 獲得超10個贊
這是一個正確的答案:
<a class="btn" ng-click="remove($index)">Delete</a>$scope.remove=function($index){ $scope.bdays.splice($index,1); }
我認為這是錯誤的,因為你傳遞$index
了參數,但你在控制器中使用了愿望。如我錯了請糾正我 :)
- 3 回答
- 0 關注
- 807 瀏覽
相關問題推薦
添加回答
舉報
0/150
提交
取消