Replacement of $parent in AngularJS

牧云@^-^@ 提交于 2020-01-05 08:33:37

问题


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.



来源:https://stackoverflow.com/questions/18282700/replacement-of-parent-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!