ng-options and $$hashKey

1,048 views
Skip to first unread message

Maxence Warzecha

unread,
Oct 5, 2012, 9:00:52 AM10/5/12
to ang...@googlegroups.com
Hi,

Short :

I would like to auto select the option in select element using "plain json" object instead if just the key.

Examples :

OK using the key in ng-options :

KO using plain json object in ng-options :

Why ?

If we select 'Blort', we can see that $$hashKey in the selectedItem are different before and after the selection. So I think the issue is related to $$hashKey.

I think it's correct but I wonder if there is a way to do 'auto' select using plain json object? Maybe it's a wrong idea? Any advice on this is welcome.

Thank you, 

Pawel Kozlowski

unread,
Oct 5, 2012, 1:17:24 PM10/5/12
to ang...@googlegroups.com
Hi Maxence!

On Fri, Oct 5, 2012 at 3:00 PM, Maxence Warzecha <war...@gmail.com> wrote:
> I would like to auto select the option in select element using "plain json"
> object instead if just the key.
>
> KO using plain json object in ng-options :
> http://jsfiddle.net/laguiz/4geGY/3/

Here is the working version: http://jsfiddle.net/pkozlowski_opensource/BXQd9/3/

> Why ?

The select directive will use object identity comparison and not
object equality.
I was trying to see if we can use a custom $$hasjKey in the ng-options
but it didn't seem to work:
http://jsfiddle.net/pkozlowski_opensource/bYpG5/5/


Cheers,
Pawel

--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Eddie Huang

unread,
Oct 5, 2012, 5:38:59 PM10/5/12
to ang...@googlegroups.com
I've also hit this issue, the built in object syntax just doesn't work well in practice

Maxence Warzecha

unread,
Oct 6, 2012, 8:21:22 AM10/6/12
to ang...@googlegroups.com
Hi!

Thank you for replies.

@Pawel Kozlowski
I see the alternative using $scope.sourceList[1]In fact this way we have the save Javascript object an therforre the same hashkey.
Using this 'trick', what about loading objects asynchronously? In fact when should I loop on the source list to override the object into the model?
In the example, it could be the case for $scope.selectedItem and $scope.sourceList are loaded using a ressource for example. 

For now I think I will use normal Keys (first example) and if I need more information based on the IDs, I will create a function that give me the 'source object' from the Array based on the ID. For this if you have recommendation to manage kind of HashMap in Javascript :-)

@Eddie Huang
I see your issue. It seems similar.

I'll try to investigate more on this issue...

Thank you for help,
Max 
Reply all
Reply to author
Forward
0 new messages