Angular 1.2.0: Error: "Reference to private fields in Angular expressions is forbidden" when trying to access Mongo _id fields

When trying to read the 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 issued:

 "Referencing private fields in Angular expressions is disallowed" 

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

EDIT:

This change was returned in angular 1.2.1: https://github.com/angular/angular.js/commit/4ab16aaaf762e9038803da1f967ac8cb6650727d

+7
angularjs mongodb
source share
1 answer

Current solution

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

It is easy, if not annoying, to fix it.

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

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

Then in your markup, use this 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> 

Corrected plunker: http://plnkr.co/edit/NcRrKh

Discussion

This will allow you to move forward with your development, but be aware that it comes with more overhead than if you could just access the variable directly. For small projects, this should not affect performance at any tangible level, although in large applications you can see a certain amount. However, this problem is now purely academic.

+6
source share

All Articles