Replacement of $parent in AngularJS

2019-07-24 13:47发布

问题:

Referencing my earlier post, why is the code using $parent here <fm-rating ng-model="$parent.restaurant.rating" readonly="true"></fm-rating>. The full code is here.

I have been reading blogs which mention to avoid using $parent, hence my question. Is there a better way to write that code? Or is there no way, since the filters on the home page need to affect the results as seen in this screenshot of the app

回答1:

fm-rating declares an isolated scope, as seen here, which means it does not use prototypical inheritance to get access to the parent properties.

This is useful when you want a scope in which you can use any names without worrying about overriding a parent property. The only way to "break out" of this isolation is to use $parent, a property on the isolated scope that refers to the parent of the scope!

For more information, search for "isolate" in the documentation of directives.