AngularJs pass instance of each ng-repeat in HTML to directive

孤街醉人 提交于 2019-12-04 06:15:17

SOLUTION: Remove the whole scope property from your directive and everything should work as expected.

ALSO: You'll need to rename the scope argument from this line:

elem.bind('click', function(scope) {

to something like:

elem.bind('click', function(e) {

because you are overwriting access to scope in your event handler by using the same name.

EXPLANATION:

The ng-repeat directive causes each of its clones to have their own new scope. Since directives on an element share scope by default, any other directives placed alongside the ng-repeat share its scope and have access to the current iteration's $scope variables. In other words, your custom directive already shares scope with ng-repeat and has access to flowObj by default.

The reason it didn't work when specifying a scope property on your custom directive is that this caused the directive to have its own isolate scope which did not share with ng-repeat and so you did not have access to variables on the clones' scopes.

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