Variable returning Undefined

Asked

Viewed 50 times

1

I need to reclaim the value of one p which sits within one of my Components, that p receives the value of my API when the page is loaded.

To do this, I made one [(ngModel)]="user" and created a variable user that should receive this value, but in my console.log it is returned as Undefined.

Paragraph:

<p class="card-text" [(ngModel)]="usuarioDestino" name="usuarioDestino" ngDefaultControl>ID:{{usuario.id}}</p>

usuarioDestino: string;

The value of this P is loaded in onInit, so as soon as the page loads it is already there.

Can you help me?

Complete code:

import { Component,  OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { UsuariosService } from '../../services/usuarios.service'
import { ModalComponent } from '../modal/modal.component';
@Component({
  selector: 'app-lista-usuarios',
  templateUrl: './lista-usuarios.component.html',
  styleUrls: ['./lista-usuarios.component.css']
})
export class ListaUsuariosComponent implements OnInit {

  usuarios: Array<any> = new Array();
  usuarioDestino: string;


  constructor(private usuariosService: UsuariosService, private dialog: MatDialog ) { }

  ngOnInit(): void {
    this.getUsuarios();
    console.log(this.usuarioDestino);
  }


  getUsuarios(){
    this.usuariosService.userList().subscribe((resposta) => {
      this.usuarios = resposta;
      console.log(this.usuarios);
    });
  }


  openDialog(): void {
    const dialogRef = this.dialog.open(ModalComponent, {
      width: '800px',
    });
   
    const destino = this.usuarioDestino;
    console.log('Destino:', destino);
    
    

    dialogRef.componentInstance.usuarioDestino = this.usuarioDestino;

    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
    });
  }

}

HTML:

<div class="w-100 containerFlex" *ngFor="let usuario of usuarios">
    <div class="card mb-3">
        <div class="row no-gutters">
          <div class="col-md-4">
            <img src="{{usuario.img}}" class="card-img" alt="...">
          </div>
          <div class="col-md-8">
            <div class="card-body">
              <h5 class="card-title">Nome de usuário: {{usuario.name}}  </h5>
              <p class="card-text" [(ngModel)]="usuarioDestino" name="usuarioDestino" ngDefaultControl>ID:{{usuario.id}}</p>
              <p class="card-text">{{usuario.username}}</p>
              <button class="btn btn-primary" data-toggle="modal" (click)="openDialog()">Pagar</button>
            </div>
          </div>
        </div>
      </div>
</div>
  • I believe that the console.log you call is not in the same context of the variable’s performance

  • Gustavo, can you explain me better? I inserted the complete code in the publication to make it easier to identify.

  • From what I saw in the code, you did not set value for the variable in this.usuarioDestino. Your getUsuarios is set value for this.usuarios aprnas

  • 1

    Look Bruno I’m sorry once again to insist on this point, but, face back a little or two there, learn first the basics of HTML and pure Javascript, then back to learning Angular, because face it seems that you do not have the basis. HTML elements p have no attribute name, what is the sense of using ngModel in a p tag, it is used in input elements like input and select, That’s what it’s for, to make bi-directional use of the data, from the template to the TS, how do you do this in a p tag?? That way you’ll just be banging your head unnecessarily!

  • Leandrade, I put ngModel because when I executed the code, the console returned an error asking for a name attribute. An input for me would not serve in this case, this field is only for visualization, but when I call my dialog I have to send the value that this P displays in order to fill an object. This object I pick up several fields and then I do a post on my API.

  • Leandrade, do you have a suggestion how I can fix this? This P gets a value from my API which is user.id. I have to display this API value in my application, and I also need to send this value to my dialog, which tag I should use?

  • I made a change, instead of using ngModel I used Elementref, but it only takes the first user from my list. I need to get the model user I clicked.

  • In code posted at no time the variable user receives a value, as you hope its value is not undefined?

Show 3 more comments
No answers

Browser other questions tagged

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