Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

ngRepeat: property names starting with "$" will not be rendered #6266

Closed
mgmeyers opened this issue Feb 14, 2014 · 14 comments
Closed

ngRepeat: property names starting with "$" will not be rendered #6266

mgmeyers opened this issue Feb 14, 2014 · 14 comments

Comments

@mgmeyers
Copy link

When using ngRepeat to iterate over an object, it will not output any key-value pair where the key starts with a dollar sign ($).

Here is a demo:
http://jsfiddle.net/takvg/4/

And, the actual code in question can be found here: https://github.com/angular/angular.js/blob/master/src/ng/directive/ngRepeat.js#L284

if (isArrayLike(collection)) {
  collectionKeys = collection;
  trackByIdFn = trackByIdExpFn || trackByIdArrayFn;
} else {
  trackByIdFn = trackByIdExpFn || trackByIdObjFn;
  // if object, extract keys, sort them and use to determine order of iteration over obj props
  collectionKeys = [];
  for (key in collection) {
    if (collection.hasOwnProperty(key) && key.charAt(0) != '$') {
      collectionKeys.push(key);
    }
  }
  collectionKeys.sort();
}

Specifically:

key.charAt(0) != '$'

I'm assuming this is to prevent angular's internal properties from being iterated over by ngRepeat. I would suggest removing this limitation, but I don't know enough about the internals or the reasoning behind this to know what effect that might have.

@ashleygwilliams
Copy link

hey @mgmeyers! thanks for submitting this issue!

however, i think the cost of implementing this would out-weigh the benefits. is there a specific scenario where you are being forced to use a hash with a key that begins with a '$'? if so, please reply back and we can reopen and discuss.

@DexterMorg4n
Copy link

Yes, there are many. $ is a common prefix for other frameworks that work with front end frameworks like Angular. That makes angular useless in creating dashboard config panels, route configuration for other software, JSON editors used for many many developments

@rodyhaddad rodyhaddad reopened this Jun 25, 2014
@rodyhaddad
Copy link
Contributor

I believe this should be fixed, or at least discussed more

@rodyhaddad rodyhaddad added this to the 1.3.0 milestone Jun 25, 2014
@btford btford removed the gh: issue label Aug 20, 2014
@IgorMinar
Copy link
Contributor

fiddle updated to 1.3.0: http://jsfiddle.net/takvg/4/

@IgorMinar
Copy link
Contributor

maybe we could make stuff we want to hide non-enumerable properties and have ng-repeat ignore those.

that would be a small code change, but the side-effects are hard to estimate, so let's not do this in 1.3.0.

@sunnypatel
Copy link

Has anyone found a workaround to this? Or is fix?

@frfancha
Copy link

@sunnypatel Workaround is easy: just make your ng-repeat on Object.keys(myObject)

@Dhumez-Sebastien
Copy link

@sunnypatel here is example of @frfancha : https://jsfiddle.net/takvg/5/

@startswithaj
Copy link
Contributor

+1

@uriva
Copy link

uriva commented Feb 16, 2016

+1

@gaurav5430
Copy link

is there a workaround for key-value pairs , where one of the keys is $ , and we want to display both key and value.

(in the example fiddle provided by Dhumez, we are only displaying keys)

@augimidge
Copy link

@gaurav5430 here is a workaround for key-value pairs.
https://jsfiddle.net/takvg/8/

@Narretz
Copy link
Contributor

Narretz commented May 9, 2018

This would require a change in angular.equals, which could possibly affect many apps negatively at this point of the AngularJS lifecycle. Closing as wontfix

@Padge91
Copy link

Padge91 commented Dec 21, 2018

In case anyone else stumbles upon this issue, this is my workaround:

I just added a null character at the front of the String value used for the key when building the object.
if (keyName.length > 0 && keyName[0]==="$"){ keyName = keyName.replace("$", "\0$"); }

Seems to work just fine (so far).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests