안녕하세요 내부 (중첩 된) observableArray에서 다음과 같이 항목을 제거하려고하지만 제거 단추가 작동하지 않습니다 (내부 foreach 항목의 경우).중첩 된 observableArray에서 항목 제거
http://jsfiddle.net/aDahT/1871/
HTML :
이<h4>People</h4>
<ul data-bind="foreach: peoples">
<li>
Name at position <span data-bind="text: $index"> </span>:
<span data-bind="text: name"> </span>
<a href="#" data-bind="click: $parent.removePerson">Remove</a>
<ul data-bind="foreach:people">
<li>
<span data-bind="text: $data"></span>
<button data-bind="click: $parent.deletePerson">Remove</button>
</li>
</ul>
</li>
</ul>
<button data-bind="click: addPerson">Add</button>
스크립트 :
var Person = function(name, children){
var self = this;
self.name = name;
self.people = ko.observableArray(children);
self.deletePerson = function() {
alert(JSON.stringify(self));
self.people.remove(this);
}
}
function AppViewModel() {
var self = this;
self.peoples = ko.observableArray([
new Person('Bert', ['baa', 'bbb']),
new Person('Charles', ["caa", "cbb"])
]);
self.addPerson = function() {
alert(this);
self.peoples.push(new Person("New" ,["Daa", "Dbb"]));
}.bind(this);
self.removePerson = function() {
self.peoples.remove(this);
}
}
ko.applyBindings(new AppViewModel());
누군가가 도와 드릴까요? 먼저 감사드립니다.
self.deletePerson = function(viewModel) {
self.people.remove(viewModel);
}
데모 JSFiddle :
감사합니다! 이 줄 경고 (JSON.stringify (self))에서 또 다른 질문. 왜 이름 가치를 보여 주지만 사람들을위한 가치를 보여주지 않는가? – peanut
Beacuse'JSON.stringify'는 원시 값만을 보여주고'people'은 관찰 가능한 배열이기 때문에 함수입니다. 'ko.toJSON()'을 사용하여 사람들이 값을 얻을 수 있도록 작성해야합니다.'alert (ko.toJSON (self));' – nemesv
감사합니다. – peanut