简体   繁体   中英

Binding on angular-meteor components

While i am trying to follow the tutorial, in the using-and-creating-angularjs-pipes page, the this.helpers is not working, because it is running only before the binding happens.

I dont know if this is something wrong in the tutorial or something that changed after the tutorial was written.

When i am trying to do console.info(binding property) in the this.helpers function, the result is undefined. When i am doing the same console.info in the $onInit, it is working.

How can i make it work?

The code based on the code from the tutorial, https://angular-meteor.com/tutorials/socially/angular1/using-and-creating-angularjs-pipes

class PartyCreator{
    constructor($scope){
        'ngInject';
        $scope.viewModel(this);
        this.subscribe('users');

        this.helpers({
            creator: ()=> {
                console.info(this.party) //returns undefined
                if(!this.party){
                    return '';
                }
                const owner = this.party.owner;
                if(Meteor.userId() !== null && owner === Meteor.userId()) {
                    return 'me';
                }

                return Meteor.users.findOne(owner) || 'nobody';
            }
        })
    }


    $onInit(){
        console.info(this.party) //returns valid party object
    }
}

Helpers run multiple times, basically every time the data changes, or when the subscription is enabled. Your code should allow for the possibility that no data is returned (usually first time).

There is code already present to deal with this case:

       if(!this.party){
            return '';
        }

When it runs again it will usually give you what you expect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM