Looks like a bug in knockout 3.5.0. The same code works with version 3.4.2, have a look at the abbreviated example below:
function RootViewModel(){
var self = this;
self.Items = ko.observableArray([1, 2, 3, 4, 5]);
self.BeforeRemove = function(element, ind, item) {
console.log("Index: " + ind);
$(element).remove();
};
};
ko.applyBindings(new RootViewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<div class="container p-5">
<button data-bind="click: function () { Items.push(Items().length + 1) }">Add</button>
<ul data-bind="foreach: { data: Items, beforeRemove: BeforeRemove }">
<li data-bind="text: $data, click: function (i) { $root.Items.remove(i) }"></li>
</ul>
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…