0

I have this 'Username' input:

<div class="form-group"> 
    <label for="userName">Username</label>
    <input name='userName' ngModel id="userName" type="text" 
           ng-model="userName"   init="userName='Bob'" value="Bob" >
</div>

I want to set the input to 'Bob' by default but this doesn't work. If i delete 'ngModel' it works and I need it otherwise it won't sent my username to server.

This is in the begging of the form(the action that happens when I press submit button):

<form #physicians="ngForm" (ngSubmit)="onFormSubmit(physicians.value)">

And this the controller:

onFormSubmit(data) {
    console.log(data);
    this.adminService.updateUser(data).subscribe((responseData: any) => {
        this.router.navigate(['/']);
   },
   (err: HttpErrorResponse) => {
       this.isSignupError = true;
       this.errorText = err.error['message'];
   });
}

How can I fix this?Thank you!

8
  • which angular version you are using Commented Sep 17, 2018 at 10:38
  • Angular 6 is what I use. Commented Sep 17, 2018 at 10:39
  • then why not you use formcontrolname Commented Sep 17, 2018 at 10:40
  • I don't know exactly how. I google it now. Commented Sep 17, 2018 at 10:41
  • let me give you code for that Commented Sep 17, 2018 at 10:41

5 Answers 5

2

you should do something like:

ts code:

export class AppComponent  {
  userName;
  constructor() {
    this.userName = 'Bob';
  }
}

html:

 <form #form="ngForm" novalidate>
    <label>userName:</label>
    <input type="text" name="userName" [(ngModel)]="userName" required minlength="2" maxlength="10" #userNameDir="ngModel">
    <pre>errors: {{userNameDir.errors | json}}</pre>
  <button type="button" [disabled]="form.invalid" (click)="onFormSubmit(form.value)">submit</button>
  <pre>form.value: {{form.value | json}}</pre>
  <pre>form.errors: {{form.errors | json}}</pre>
  <pre>form.status: {{form.status | json}}</pre>
  <pre>form.dirty:{{form.dirty | json}}</pre>
</form>

DEMO.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use

[defaultValue]="myDefaultValue"

without Angular changing the variable 'myDefaultValue' on the input change.

Comments

0

Try using the following code: HTML:

<input name='userName'  id="userName" type="text" [(ngModel)]="userName">

TS:

userName='Bob' // when you have declared the variable

Comments

0
 <form [formGroup]="physicians" (ngSubmit)="onSubmit(physicians.value)">
<div class="form-group"> 
            <label for="userName">Username</label>
            <input  name='userName'  type="text"  formControlName="userName"  >
     </div>

TS FILE code

Put this in ngOnInit

this.physicians= this._formBuilder.group({
      userName:['bob']
})



onFormSubmit(data) {
console.log(data);
 this.adminService.updateUser(data).subscribe((responseData: any) => {
  this.router.navigate(['/']);
},
(err: HttpErrorResponse) => {
  this.isSignupError = true;
  this.errorText = err.error['message'];
});

Comments

0

you can add this.userName = 'Bob'; in the file .ts , remove init and value='Bob' from html.

Comments

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.