KnockoutJs에서 사용하는 JavaScript 객체 리터럴이 있으며 필요한 속성으로 가져올 수없는 함수 안에 있습니다. 이건 내 현재 자바 스크립트입니다함수 내부에서 속성에 액세스하는 방법?
var vm = function() {
var self = this;
function remove(item) {
self.myArray.remove(item)
};
}
:
var vm = {
myArray: ko.observableArray([]),
remove: function (item) {
//debugger; Cant get to myArray
//this is the item of the array
myArray.remove(item);
} // .bind(this) doesn't work
}
및 HTML :
<!-- ko foreach: myArray-->
<button type="button" data-bind="click: $parent.remove, uniqueName: true" >Remove</button>
...
<!-- /ko -->
'this'는 함수가 호출되는 방식에 따라 다릅니다. 'vm.remove()'와 같은 함수를 호출하면'remove' 안에서'this'가'vm'이됩니다. Knockout에 익숙하지 않지만 단순히 'this.myArray.remove (item)'를 사용하여 시도해 보셨습니까? – elclanrs