[英]Knockout.js Mapping and adding large dataset to ko.observableArray
[英]Knockout.js ko.observableArray() not working as espected
我不確定為什么這行不通,有人可以幫忙嗎?
我有這個簡單的對象數組:
myItems: [
{value: 0, text: 'a'},
{value: 1, text: 'b'},
{value: 2, text: 'c'}
],
allItems: ko.observableArray(this.myItems),
console.log(this.allItems()); THIS DOES NOT WORK???
以上不起作用?
現在,下面只是硬編碼的myItems數組:
allItems: ko.observableArray([
{value: 0, text: 'a'},
{value: 1, text: 'b'},
{value: 2, text: 'c'}
]),
console.log(this.allItems()); //THIS NOW WORKS
兩者都登錄時看不到有什么區別:
console.log('this is al allItems: '+this.allItems())
console.log('this is my myItems: '+myItems());
this is al allItems: (3) [{…}, {…}, {…}]
this is my myItems: (3) [{…}, {…}, {…}]
任何幫助,請感謝。
似乎在初始化對象時,您正在將其值分配給另一個鍵。 在給定的代碼段中, this
是指不存在myItems
鍵的全局對象,並返回undefined
。 嘗試在瀏覽器中執行var a = {b: 1, c: this.b}
。 這將返回{ b: 1, c: undefined }
。
但是我的問題是,為什么要分開保存myItems
又不想將其值直接傳遞給ko.observableArray
函數? 因為以后你只會想處理allItems
,因為它是可觀察的,不是myItems
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.