Drop Down list data binding Angular 2

272 views
Skip to first unread message

AJ Tamminen

unread,
May 4, 2016, 2:10:01 PM5/4/16
to AngularJS
Hi!

Have a really simple question that's driving me nuts! I got it to work once, but managed to make the code disappear...

I have input field and a drop down list. When the user writes text in the input field and chooses value from list, they are added to an object, AND when the user navigates around the page and returns back to this page, the input and value chosen are visible. As said, the ngmodel for keyword works fine, and it still exists when returning back to page. But how do I implement the model to the dropdown list, so it works as well? I've tried sooo many different things, but can't do it anymore ;)

This works fine:
<input required [(ngModel)]="keyword">

Need help here!
<select>
   <option *ngFor="#v of possibleValues">{{v}}</option>
</select>

Zlatko Đurić

unread,
May 5, 2016, 2:13:26 AM5/5/16
to AngularJS
It sounds too simple to be ok, but... Don't you need an ngModel on <select> too? I have a <select [(ngModel)]="variable"> here that works when "variable" is set programatically, so I'd expect you need the same?

Radu Florin

unread,
May 5, 2016, 9:14:10 AM5/5/16
to AngularJS
I would bind to an event (mouseLeave) and call a function, in that function check if the element keyword in the input is valid and then push it to the possibleValues array.
Reply all
Reply to author
Forward
0 new messages