ngModel and #firstName always fails with ". . not assignable to . ."

Working through Mosh’s course on the ngModel lecture I’m getting the following error:

Argument of type ‘NgModel’ is not assignable to parameter of type ‘string’.

This is happening whenever I add change detection output.

(change)="log(firstName)"

To this:

<input ngModel name="firstName" #firstName="ngModel" id="firstName" type="text" class="form-control">

I’m stumped on how to move forward. Suggestions welcome.

Thanks.

Fixed. The issue was in the component.ts:

log( msg : string ) {
    console.log( msg );
}

The template variable #firstName is an object (not a string).

To show the value of the template variable use:

(change)="log(firstName.value)

or be prepared for an object to be passed to the log() function.

2 Likes

Second update for completeness.

As the template variable type isn’t known I found it required to specify the variable type any otherwise an error would occur (Parameter ‘msg’ implicitly has an ‘any’ type.)

log( msg : any ) {
    console.log( msg );
}
1 Like