2

Getting [Object Object] in angular form and also dropdown(select) not set to default 0 index. While in case of edit everything working fine. Do I need to initialize model properties for this create page?

enter image description here

Edit : ngModelChange not calling the function doNameChange.

Component:

  export class User {
  Id: number;
  Name: string;
  Gender: string;    
}
this.user = {
      Id: 0,
      Name: '',
      Gender: ''
}
doNameChange(event) {
    debugger;
    console.log(event); // logs model value
  }

Html:

 <form (ngSubmit)="save(f.value, f.valid)" #f="ngForm" novalidate materialize>      
      <input id="Name" (ngModelChange)="doNameChange($event)" name="Name" #Name="ngModel" type="text" class="validate form-control" required minlength="3" [(ngModel)]="user.Name">
      <select id="Gender"  name="Gender" #Gender="ngModel" class="validate form-control" [(ngModel)]="user.Gender" required>
          <option value="">-- Select Gender --</option>
          <option value="Male">Male</option>
          <option value="Female">Female</option>
        </select>
    </form>
4
  • Remove ngmodel when you use reactive forms` Commented Jun 6, 2018 at 6:09
  • @Vega, I want to want to watch an object change.. wt to do then? Commented Jun 6, 2018 at 6:12
  • You look at the documentation for both, and chose one. And please, be more specific about your issue, because apart from the usage of both template driven forms and reactive forms, I don't see any issue in your code. Commented Jun 6, 2018 at 6:31
  • ok @trichetriche will take care of it.. Commented Jun 6, 2018 at 7:00

1 Answer 1

1

While using reactive forms, don't use ngModel which is for template based forms. If you need to listen to the changes:

ngÒnInit(){
   this.employeeForm = this._fb.group({
      EmployeeId: 0,
      Name: ['', [Validators.required, Validators.minLength(3)]],
      Gender: ['', [Validators.required]]  //dropdown
   });
   this.onChanges();
}
...

onChanges(): void {
  this.myForm.valueChanges.subscribe(val => {
        console.log(val);
  });
}

 //or:
onChanges(): void {
  this.myForm.get('name').valueChanges.subscribe(val => {
    console.log(val);
  });
}
Sign up to request clarification or add additional context in comments.

8 Comments

not working @Vega, I have modified question and added(in Edit part) what I have changed as per your answer..
In my answer I suggested to remove ngModel. As for the code you pasted, it should work. What is not working? It doesn't follow the changes? Could you make a demo so I can see the rest of your class?
What doesn't work? What do you expect? It seems to work for me: stackblitz.com/edit/angular-rktfh8
not printing value on console, don't know why.. Do it require any declaration or any import.. is this hello component required to in my app?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.