How to bind a Promise to a component property?

Downhillski

I have a promise object needs to be parsed to another component, how to achieve this so that when the promise in component-one is resolved, the promise object parsed to component-two can also be resolved?

ComponentOne excerpt

@Component({
  selector: 'component-one',
  template: `
      <h1>Title</h1>
      <component-two [promisedData]="promisedData"></component-two>
    `,
  directives: [ComponentTwo]
  })
export class ComponentOne {
  promisedData: Promise<any>;

  constructor () {
    this.promisedData = new Promised(resolve => {
             setTimeout(function(){
                 resolve('Test');
              }, 1000);
    });
  }
}

ComponentTwo excerpt

@Component({
  selector: 'component-two',
  template: `
      <h2>
        {{processedData}} 
        // the {{processedData}} is undefined becaused the 
        // resolved promise doesn't get to parse to the component-two.
      </h2>
    `,
  inputs: ['promisedData']
  })
export class ComponentTwo {
  promisedData: Promise<any>;
  processedData: string;

  constructor () {
    PromisedData.then(data => this.processedData = data);  
  }
}
Mark M

The easy way is to just add the async pipe to your template like this:

{{promisedData | async}}

Edit: here's a working example showing the async pipe: plunker

Edit2: Plunker showing OnInit: plunker

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to bind textblock with property

From Dev

How to Bind Attached Property

From Dev

How to bind dynamic property?

From Dev

How to Bind the Property of an Instance?

From Dev

How to bind a ContentView to a property?

From Dev

Angular 2 - how do I data bind an SVG attribute to a component property?

From Dev

Bind a service property to a component property with proper change tracking

From Dev

How to bind to a property in view but not in viewmodel?

From Dev

How to bind property using a formatter

From Dev

How to bind a textbox to class Property

From Dev

How to bind control to object property?

From Dev

How to bind property of a ObservableCollection in XAML

From Dev

How to bind to attached property in UWP?

From Dev

How to bind control to object property?

From Dev

How to bind ComboBox to ObservableCollection property?

From Dev

How to bind checkBox to a boolean property?

From Dev

How to bind user control property to other property?

From Dev

How to bind content to individual div with promise

From Dev

Bluebird, Promise.bind - how to access bound context in pending promise?

From Dev

Bind different property than what is shown in the html component

From Dev

How to observer base component property

From Dev

In Angular 1.5, how to bind an attribute component as boolean?

From Dev

How to bind components to component after using forwardRef?

From Dev

React.js, how to bind `this` outside of component

From Dev

vueJS 2, how to bind javascript to a component dynamically

From Dev

how to bind component data to a value from the prop

From Dev

How to bind redux dispatch method to a "dumb" component

From Dev

How to bind a multiple word attribute in an AngularJS component

From Dev

How to bind an array to the component template in Polymer 2?

Related Related

  1. 1

    How to bind textblock with property

  2. 2

    How to Bind Attached Property

  3. 3

    How to bind dynamic property?

  4. 4

    How to Bind the Property of an Instance?

  5. 5

    How to bind a ContentView to a property?

  6. 6

    Angular 2 - how do I data bind an SVG attribute to a component property?

  7. 7

    Bind a service property to a component property with proper change tracking

  8. 8

    How to bind to a property in view but not in viewmodel?

  9. 9

    How to bind property using a formatter

  10. 10

    How to bind a textbox to class Property

  11. 11

    How to bind control to object property?

  12. 12

    How to bind property of a ObservableCollection in XAML

  13. 13

    How to bind to attached property in UWP?

  14. 14

    How to bind control to object property?

  15. 15

    How to bind ComboBox to ObservableCollection property?

  16. 16

    How to bind checkBox to a boolean property?

  17. 17

    How to bind user control property to other property?

  18. 18

    How to bind content to individual div with promise

  19. 19

    Bluebird, Promise.bind - how to access bound context in pending promise?

  20. 20

    Bind different property than what is shown in the html component

  21. 21

    How to observer base component property

  22. 22

    In Angular 1.5, how to bind an attribute component as boolean?

  23. 23

    How to bind components to component after using forwardRef?

  24. 24

    React.js, how to bind `this` outside of component

  25. 25

    vueJS 2, how to bind javascript to a component dynamically

  26. 26

    how to bind component data to a value from the prop

  27. 27

    How to bind redux dispatch method to a "dumb" component

  28. 28

    How to bind a multiple word attribute in an AngularJS component

  29. 29

    How to bind an array to the component template in Polymer 2?

HotTag

Archive