Angular 2 - Displaying async Object data from promise

前端 未结 7 871
野性不改
野性不改 2020-12-02 22:57

Edit: It looks like my main problem now is that I can\'t seem to display async data from an object. I have a promise containing the data object, and when I use



        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 23:45

    So I ended up writing my own asynchronous key pipe. Huge thanks to Simon for helping guide me here.

    import {Pipe} from 'angular2/core';
    
    @Pipe({
        name: 'key',
        pure: false
    })
    
    export class KeyPipe {
        private fetchedPromise: Promise;
        private result: string;
    
        transform(value: Promise, args: string[]) {
            if(!this.fetchedPromise) {
                this.fetchedPromise = value
                    .then((obj) => this.result = obj[args[0]] );
            }
            return this.result;
        }
    }
    
    
    

    Usage:

    {{ data | key: 'Name' }}

    Someone please comment if Angular has its own functions for resolving a key from an asynchronous object.

    提交回复
    热议问题