Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why [(ngModel)] is not working

I am a new bee to angular 2. I have seen multiple answers on stackoverflow but none of them is working in my case here is the error that it is showing

Can't bind to 'ngModel' since it isn't a known property of 'md-select'. 1. If 'md-select' is an Angular component and it has 'ngModel' input, then verify that it is part of this module.

and here is my code app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { MaterialModule } from '@angular/material';
import { NgModule } from '@angular/core';
import { FormsModule }   from '@angular/forms';
import 'hammerjs';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    MaterialModule.forRoot(),
    NavigationModule,
    FormsModule
  ],
  bootstrap: [
    AppComponent
  ]
})
**my component**
export class ToolbarComponent {
  private item: string = '3';
}

in the template of the component

<md-select [(ngModel)]="item">
  <md-option *ngFor="let v of allvalues()" [attr.selected]="v == avalue() ? true : null">{{v | translate}}</md-option>
</md-select>

please help me in solving this problem its been 2 days I am stuck at this problem.

like image 795
Muhammad Hamza Altaf Avatar asked Mar 11 '26 19:03

Muhammad Hamza Altaf


1 Answers

For anyone who ends up here I believe the anwser is that you need a name property for your input fields inorder for a two way binding to work.

<input matInput [(ngModel)]="name" id="name" name="name" placeholder="name">
like image 126
TheNetRunner Avatar answered Mar 14 '26 11:03

TheNetRunner