簡體   English   中英

更新傳遞到子組件Angular2的數據

[英]Updating data that is passed into child component Angular2

我是Angular2-流星開發的新手。 在使用父子組件時,我偶然發現了一個問題。 在父組件中,我有一個流星正在訂閱一個集合。

users: any;

constructor(private _zone:NgZone){


    var sub =  Meteor.subscribe("accounts/users");
    Tracker.autorun(function() {
        _zone.run(function() {
            if(sub.ready()){
                this.users = Meteor.users.find().fetch();
                console.log(this.users);
            }
        });
    });

}

用戶集合共有90個用戶。 最初加載應用程序時,僅找到當前用戶,因此控制台日志顯示一個用戶。

我不確定Tracker.autorun()和NgZone的放置位置是否正確,但是在加載應用程序一秒鍾后,控制台日志會顯示所有90個用戶的數組。 我認為發生這種情況是因為訂閱一開始還沒有准備好。

我的子組件將獲取的用戶作為參數,例如<sd-table [data]="users"></sd-table> 加載應用程序后,在子組件的繪制模板上僅看到一個用戶。 發生訂閱並可以訪問所有用戶時,是否可以通過任何方式更新模板?

如果要引用當前類的this function() ,請不要使用function()

users: any;

constructor(private _zone:NgZone){
    var sub =  Meteor.subscribe("accounts/users");
    Tracker.autorun(() => {
        _zone.run(() => {
            if(sub.ready()){
                this.users = Meteor.users.find().fetch();
                console.log(this.users);
            }
        });
    });
}

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM