user9855223
user9855223

Reputation:

Angular 6 - [ngModel] data value - Identifier name is not defined

I am trying to add the data value to into an ngModel.

I have the data:

  data = [
    {
      name: 'SomeName'
    }
  ];

And then on my app.component.html:

<input type="email" name="name" class="form-control" [ngModel]="data.name">

I'm getting this error:

Identifier name is not defined. Array does not contain such a member.

How can I fix this?

Upvotes: 1

Views: 2941

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222702

Data should be an Object to bind to ngModel, change it as

 data = { name: 'SomeName' };

another way is to use the index , if you do not want to change the structure of data

<input type="email" name="name" class="form-control" [ngModel]="data[0].name">

Upvotes: 0

Ashish Ranjan
Ashish Ranjan

Reputation: 12960

It should be something like:

<input type="email" name="name" class="form-control" [ngModel]="data[0].name">

You are trying to access the name property from an Array, you should reach to the Object containing that property. (Array[index])

Upvotes: 1

Related Questions