Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 1.2.0: Error: "Referencing private fields in Angular expressions is disallowed" when attempting to access Mongo _id fields

When attempting to read a mongo _id field from an angular expression:

<tr ng-repeat="person in people">
  <td>{{person._id}}</td>
  <td>{{person.name}}</td>
  <td>{{person.location}}</td>
  <td>{{person.active}}</td>
</tr>

the following error is thrown:

"Referencing private fields in Angular expressions is disallowed"

plunker link: http://plnkr.co/edit/dgHNP2rVGPwAltXWklL5

EDIT:

This change has been reverted in Angular 1.2.1: https://github.com/angular/angular.js/commit/4ab16aaaf762e9038803da1f967ac8cb6650727d

like image 531
codevinsky Avatar asked Nov 14 '13 16:11

codevinsky


1 Answers

The Current Solution

This is due to a breaking change in Angular 1.2.0 ( discussed here: https://github.com/angular/angular.js/commit/3d6a89e )

There's an easy, if not annoying, fix to this.

In your app.run function, you can add a $rootScope level accessors object that contains a function to return the correct Mongo ID.

app.run(function($rootScope) {
  $rootScope.accessors = {
    getId: function(row) {
      return row._id
    }
  }
});

Then, in your markup, use that method instead of directly accessing the data point:

<tr ng-repeat="person in people">
  <td>{{accessors.getId(person)}}</td>
  <td>{{person.name}}</td>
  <td>{{person.location}}</td>
  <td>{{person.active}}</td>
</tr>

A plunker with the fix: http://plnkr.co/edit/NcRrKh

Discussion:

This will allow you to progress forward with your development, but know that it does come with more overhead than if you could just access the variable directly. For small projects, this should not impact performance at any perceptible level, though you might see some measurable amount on larger apps. However, this concern is, as of right now, purely academic.

like image 151
codevinsky Avatar answered Sep 19 '22 12:09

codevinsky