Difficulty finding list in Angular?

Asked

Viewed 20 times

0

Take a good look at the image below;

inserir a descrição da imagem aqui

In my class of services it was like this;

 pesquisar(): Promise<any> {
        return this._http.get(`${this.url}/menus` )
            .toPromise()
            .then(response => {
              response.json(); 
          })
    }

I just need to know what my method will look like in the component class;

I tried like this but I didn’t succeed, I need help;

 menu = [];


  public title: string;

  constructor(
    private _menuService: MenuAdminService
  ) {
      this.title = 'Lista de Cardápios';
    }

  ngOnInit() {

  this.pesquisar()
  }

   pesquisar(){

    this._menuService.pesquisar()
    .then(menu => {
      this.menu = menu;
      console.log(this.menu);

    });

  }
  • this. _menuService.search() . then(menu => { this.menu = menu.data; console.log(this.menu); });

  • @Lucasbrogni gave this error message Error: Uncaught (in Promise): Typeerror: Cannot read Property 'data' of Undefined

  • take the service then.

  • @Lucasbrogni if I take out the service then the method gets error at code build time .

  • Better use observables at angular

  • @Eduardovargas because it would be better to use observables in that implementation?

  • @wladyband is the default used. Instead of returning to Promise you rereave an observable and give a subscribe on it.

  • We cannot say that Observable is a @Lucasbrogni standard, both are used in the labor market, it will depend on the implementation, in my view it does not need observable, read this => http://www.guj.com.br/t/para-a-bestapproachprojects-angular-observable-or-Promise/365774/9

  • In this case you would have to use the observable map.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.