Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

md-select with ngModel requires name attribute (hasValue error) #5402

Closed
kmturley opened this issue Jun 28, 2017 · 5 comments · Fixed by #5405
Closed

md-select with ngModel requires name attribute (hasValue error) #5402

kmturley opened this issue Jun 28, 2017 · 5 comments · Fixed by #5405
Assignees

Comments

@kmturley
Copy link

Bug, feature request, or proposal:

This is a bug with the documentation not matching expected behaviour

What is the expected behavior?

No error when copying guide code

What is the current behavior?

You get an error:
MdSelect.html:1 ERROR TypeError: Cannot read property 'hasValue' of undefined
at Object.View_MdSelect_0.co [as updateDirectives]

What are the steps to reproduce?

Following the guide, and copying example code should render without errors:
https://material.angular.io/components/select/overview

<md-select placeholder="State" [(ngModel)]="myState">
   <md-option *ngFor="let state of states" [value]="state.code">{{ state.name }}</md-option>
</md-select>

What is the use-case or motivation for changing an existing behavior?

The error is not useful, also modifying the example code, adding a name attribute to the md-select element fixes the issue:

<md-select placeholder="State" [(ngModel)]="myState" name="states">
   <md-option *ngFor="let state of states" [value]="state.code">{{ state.name }}</md-option>
</md-select>

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 4.0.2
Angular Material 2.0.0-beta.7
MacOS 10.12.5
TypeScript 2.0.0
Chrome 59.0.3071.109

Is there anything else we should know?

The other example is correct here:
https://material.angular.io/components/select/examples

@willshowell
Copy link
Contributor

It works fine here without the name attribute. Can you reveal more about your setup?

As a note: Typescript 2.2 is required (not sure if it's noted anywhere in the docs or is relevant here)

@crisbeto
Copy link
Member

The name requirement on elements with ngModel comes from @angular/forms and only applies if the element is inside a form.

@kmturley kmturley changed the title md-select with ngModel requires name attribute (other errors) md-select with ngModel requires name attribute (hasValue error) Jun 28, 2017
@crisbeto crisbeto self-assigned this Jun 28, 2017
@kmturley
Copy link
Author

ah yes, can that be outlined in the guide? I would guess it's a common use case

@crisbeto
Copy link
Member

I'd say the message is helpful enough since it gives an example of how to solve the error. I'll submit a fix so Material doesn't throw any extra errors that may cause confusion.

crisbeto added a commit to crisbeto/material2 that referenced this issue Jun 28, 2017
If the user has an `md-select` with an `ngModel` that doesn't have a name inside a form, the forms module will throw an error, however Material will also start throwing errors, which may cause confusion. These changes add a null check so our errors don't get mixed up with the forms error.

Fixes angular#5402.
crisbeto added a commit to crisbeto/material2 that referenced this issue Jun 29, 2017
If the user has an `md-select` with an `ngModel` that doesn't have a name inside a form, the forms module will throw an error, however Material will also start throwing errors, which may cause confusion. These changes add a null check so our errors don't get mixed up with the forms error.

Fixes angular#5402.
crisbeto added a commit to crisbeto/material2 that referenced this issue Jun 29, 2017
If the user has an `md-select` with an `ngModel` that doesn't have a name inside a form, the forms module will throw an error, however Material will also start throwing errors, which may cause confusion. These changes add a null check so our errors don't get mixed up with the forms error.

Fixes angular#5402.
mmalerba pushed a commit that referenced this issue Jul 7, 2017
If the user has an `md-select` with an `ngModel` that doesn't have a name inside a form, the forms module will throw an error, however Material will also start throwing errors, which may cause confusion. These changes add a null check so our errors don't get mixed up with the forms error.

Fixes #5402.
mmalerba pushed a commit that referenced this issue Jul 9, 2017
#5405)

If the user has an `md-select` with an `ngModel` that doesn't have a name inside a form, the forms module will throw an error, however Material will also start throwing errors, which may cause confusion. These changes add a null check so our errors don't get mixed up with the forms error.

Fixes #5402.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants