2016-11-15 73 views
0

我有一個angular2-流星的項目,我有這個對象的colecction:更新數組值被動

{ name:"name1", value:4 } 

,我想value屬性像數組:

[1, 2, 3, 4] 

,我想集合中出現的變化反應出現在我的數組上。例如:如果我在我的集​​閤中插入一個新文檔,我想在我的數組值中添加一個新項目。 在我的客戶端我訂閱了這個集合是這樣的:

this.collectSub = MeteorObservable.subscribe('collection', options).subscribe(() => { 
    this.objects = MyCollection.find({}, { 
     sort: { 
     name: nameOrder 
     } 
    }).zone(); 
    }); 

我嘗試過this.object使用地圖功能,但這不工作。

任何幫助,歡迎。由於

回答

0

你試試這個:

this.collectSub = MeteorObservable.subscribe('collection', options).subscribe(() => { 
    this.array = MyCollection.find({}, { 
     sort: { 
     name: nameOrder 
     } 
    }).map((item) => item.value).zone(); 
    }); 

,如果你不希望使用地圖上的光標,你可以使用地圖這樣的:

let collectionMock = [{name:"name1", value:1},{name:"name2", value:2},{name:"name3", value:3},{name:"name1", value:4}]; 
let result = collectionMock.map((item) => item.value); 

我真的不知道該角的方式,但我認爲,如果「this.objects」是一個ReactiveVar你可以用Tracker.autorun包裝你的地圖執行,如:

Tracker.autorun(() => { 
    this.resultArray = this.objects.map((item) => item.value);  
    });