Angular 4 using subscribe to bind data from a promise with an observable not working -


hello have pouchdb service retrieves data , return promise it. want convert promise observable , subscribe in ngoninit model gets updated.

i made implementation of service method returns observable, it's not working (the data not reflected on dom):

component html:

<h1>{{title}}</h1> <ul>   <li *ngfor="let platform of platforms">     <a href="#">{{ platform }}</a>   </li> </ul> 

component ts:

import { component, oninit } '@angular/core'; import { pouchdbbuilder } './pouchdbbuilder'; import { platformsservice } './services/platformsservice';  @component({   selector: 'platforms',   templateurl: './platforms.component.html' }) export class platformscomponent implements oninit {   title : string;   platforms: array<string>;   private platformsservice;    constructor() {     this.title = "all platforms";     let servicename = 'http://xxxx.net:5984/';     let pouchdbbuilder = new pouchdbbuilder(servicename);     this.platformsservice = new platformsservice(pouchdbbuilder);   }    ngoninit() {     this.platformsservice.getplatforms().subscribe(platforms => {       this.platforms = platforms;     });   } } 

service ts:

 import { injectable } '@angular/core';     import {pouchdbbuilder} './pouchdbbuilder';     import {platformsdataprovider} './platformsdataprovider';     import { observable } "rxjs/observable";      @injectable()     export class platformsservice {      private platformsprovider;      constructor(private pouchdbbuilder: pouchdbbuilder) {       this.platformsprovider = new platformsdataprovider(this.pouchdbbuilder);     }      getplatforms(): observable<array<string>> {      return new observable(observer => {       this.platformsprovider.getplatforms().then(platforms => {         observer.next(platforms);       }).catch(err => {         console.error(err);       });     });    }   } 

this works if add "this.ngzone.run" after suscribe ngoninit, this:

ngoninit() {     this.platformsservice.getplatforms().subscribe(platforms => {       this.ngzone.run(() => {         this.platforms = platforms;       });     });   } 

but way it's cheating because i'm forcing update on dom.

can tell me i'm doing wrong?

import 'rxjs/add/observable/frompromise';  var subscription = observable.frompromise(your promise data); subscription.subscribe(data => /* data received */,                        error => /* handle error here */); 

update

   var promise = this.platformsprovider.getplatforms().then(platforms => {         return platforms.json();       });   return observable.frompromise(promise); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -