-
Notifications
You must be signed in to change notification settings - Fork 25.6k
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
How to use select/option/NgFor on an array of objects? #4843
Comments
@vsavkin I think we need to have something like |
Any news on this? |
Maybe problem with export class SelectControlValueAccessor implements ControlValueAccessor {
value: string; // Does typescript has any convert rules?
onChange = (_) => {};
onTouched = () => {};
...
writeValue(value: any): void {
this.value = value;
this._renderer.setElementProperty(this._elementRef, 'value', value);
} |
I have run into this problem as well. NgFor does not seem to work correctly inside a |
We're experiencing this too. We'd like to be able to use ngFor inside of a select and access the object representing the option when it's selected. |
This works for test case 2:
bar gets displayed on render Or set
To render the name and just return the value. What doesn't seem to work is setting
bar gets rendered, but strValue is strangely now an object The [(ng-model)] syntax was throwing errors for me. I'm new to ng-2 ... |
Actually, wouldn't setting [(ngModel)] to "objValue.name" change the name of objValue and not change the selected object when the user chooses another entry in the select box? i.e.: initial: after selecting the second entry: |
Test case 2 seems logical to me, but still doesn't work in beta.1. The value appears to be converted to a string, which results in |
While looking at the angular2 tutorial I came across this bug while playing with the sample code https://angular.io/resources/live-examples/forms/ts/plnkr.html |
+1 |
So we can't use select. Maybe we should use radio buttons. Oh , no wait, those are broken too |
Of course you can use Snippets:
|
I could not get the above hack from @slintes to work at first. Interesting to note that the order of the ngModel and the ngModelChange is important. For reference, we are using 2.0.0-beta.3. |
I confirm that [(ngModel)] and (ngModelChange) on select are not working in Firefox and Edge. Any plans on fixing this? |
oh no, you're right, I only tested in Chrome until now. |
The Firefox and IE select bug is a separate issue: #6573 |
I think this issue may cause by the two-way binding |
@slintes , thanks a lot for the workaround. |
Any updates on when this is going to get fixed? |
+1 having to workaround with using stringify on objects right now. |
OK, I have escalated it. |
Thank you, @kara! I look forward to trying this out. |
Update (once #7939 goes in): To use a select with objects as option values, use <select [(ngModel)]="selectedCity">
<option *ngFor="#city of cities" [ngValue]="city">{{city.name}}</option>
</select> class MyComp {
selectedCity: Object;
cities: Object[] = [
{name: "SF"},
{name: "NYC"}
];
} You can still use |
I tried the ng-value code approach, including the way the objects are set
->][ng-value]="country.name">
"): PostApartment4Rent@20:55
state.name">
"): PostApartment4Rent@29:50 Here's how one of the objects is set: On Wed, Apr 6, 2016 at 5:44 PM, Kara notifications@github.com wrote:
Mike Laird 12 Arbor Creek Drive, Pittsford, New York 14534 ". . . remembering on both sides that civility is not a sign of weakness, |
@mLaird change |
Thanks. With ngValue, the page loads, no Console errors, validation works, On Mon, Apr 11, 2016 at 2:17 PM, Pascal Precht notifications@github.com
Mike Laird 12 Arbor Creek Drive, Pittsford, New York 14534 ". . . remembering on both sides that civility is not a sign of weakness, |
I now discovered that everything works fine (all of the controls object is On Mon, Apr 11, 2016 at 3:35 PM, Michael Laird michael.w.laird@gmail.com
Mike Laird 12 Arbor Creek Drive, Pittsford, New York 14534 ". . . remembering on both sides that civility is not a sign of weakness, |
@mLaird please file a separate issue for FF. |
Thanks. The new issue is On Tue, Apr 12, 2016 at 1:47 PM, Miško Hevery notifications@github.com
Mike Laird 12 Arbor Creek Drive, Pittsford, New York 14534 ". . . remembering on both sides that civility is not a sign of weakness, |
Hi i am new to angular2 and i have a small task which i am not getting.i want to display details from database by selecting a value in drop down list and date . how should i display using webapi service or normal service.please try to help me in this problem thanks in advance |
ankasala, post your question and your current code at stackoverflow.com On Wed, Jul 13, 2016 at 11:10 AM, ankasala notifications@github.com wrote:
Mike Laird 12 Arbor Creek Drive, Pittsford, New York 14534 ". . . remembering on both sides that civility is not a sign of weakness, |
Hi, Guys, have you tried this testing this issues with angular 2 final. My code:
My json data:
|
@calvinKG just a suggestion. |
@CaselIT I still can't get the selected option . On my HTML this is what I get. |
I can't get the selection to work as well.
The updating of ngModel on selection is working but if I set the selectedOffice variable the option isn't selected in the form control. |
i got a weird, could be similar, issue where i got a series of selects/dropdowns generated with ngFor. the number of dropdowns varies based on another dropdown. no issue rendering from intial value of the binded variable/model but weird thing is, when you change one value in a dropdown, it reflects as well in the dropdown next to it. the binded model though have the correct updated value. :) |
@CaselIT Managed to do a work around for it. Got selected value.
|
I have similar problem too.
When i change the select Any solution? |
@rkusuma full plunker to reproduce required |
@zoechi sorry it's working now. because i'm using redux, the value that i passing to |
@LMFinney changing [value] to [ngValue] solves the problem for me.
|
@belloyang Your answer works great. Thanks, you save my day. |
how about, when I select a country, country name will show. And then the country code/zip code will show automatically to the input text box.
and in my component file/TS file: countries: Object[]; I'm just new in Angular, I hope someone can help me. TIA! |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm having trouble creating a select in Angular2 that is backed by an array of Objects instead of strings. I knew how to do it in AngularJS using ngOptions, but it doesn't seem to work in Angular2 (I'm using alpha 42).
In the sample below, I have five selects, but only three of them work.
I can do (4) or (5) if that's the intended way, but they seem pretty clunky. Is there another approach? Am I just too early in the alpha? Did I do something silly?
The text was updated successfully, but these errors were encountered: