1

I want to get access input field [newUser] value in button click action [onAddUser()] in .ts file.

<input type="text" 
        ng-model="newUser"
        style="text-align:center"/>
<button (click)="onAddUser()" >Add User</button>
1
  • The code is mixing AngularJS directives with Angular 2+ directives. ng-model is an AngularJS directive. Commented Jun 24, 2018 at 16:15

1 Answer 1

3

If you are using Angular (not AngularJS 1.x) then you need to change the NgModel syntax:

In HTML (template):

<input type="text" 
         [(ngModel)]="newUser"
         style="text-align:center"/>
<button (click)="onAddUser()" >Add User</button>

In TS file:

export class YourComponent {
  newUser: string;

  onAddUser(){
    alert(this.newUser); //get the input value
  }
}

Also, remember to import the FormsModule:

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
  bootstrap: [AppComponent],
  declarations: [AppComponent],
  imports: [
    CoreModule,
    FormsModule
  ],
})
export class AppModule {}
Sign up to request clarification or add additional context in comments.

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.