Initialize a form with values of an object with ANGULAR 4

0

I'm having trouble creating a CRUD with Angular 4. I'm trying to create a form by putting the value of an object in it so I can edit it.

Here's an example:

<form #editar="ngForm"  (ngSubmit)="lerClientes()">
  <div>
    <label for="nome">Nome:</label>
    <input type="text" id="nome"   name="nome"  (ng-model)="cliente.nome"/>
    <label for="sobrenome">Sobrenome:</label>
    <input type="sobrenome" id="sobrenome" name="sobrenome" (ng-model)="cliente.sobrenome"/>
  </div> 
<div>
  <button type="submit"> botao</button>
  </div>>
</form>
    
asked by anonymous 02.05.2018 / 00:23

2 answers

0

Good morning, so you can edit the values through Typescript you need to change the (ng-model)="cliente.nome" to [(ng-model)]="cliente.nome" , because the property among the [] is just a GET to be able to set precise ones (), so you will use the two-way bind if you want to use for the other inputs that you want to change to get the value in the typescript, I forgot to speak, to appear straight in the form, it is necessary to put in typescript. For example on the counter place:

cliente.nome = "Rogério";
cliente.sobrenome = "Almeira";

And with [(ng-model)] in HTML as soon as it's changed, the value will already be saved in the variable.

    
06.09.2018 / 15:23
-1
export class AppCtorComponent {
  title: string;
  myHero: string;

  constructor() {
    this.title = 'Tour of Heroes';
    this.myHero = 'Windstorm';
  }
}
    
14.05.2018 / 17:57