How to access parent scope from within a custom directive *with own scope* in AngularJS?

后端 未结 6 677
终归单人心
终归单人心 2020-11-22 13:50

I\'m looking for any manner of accessing the \"parent\" scope within a directive. Any combination of scope, transclude, require, passing in variables (or the scope itself)

6条回答
  •  星月不相逢
    2020-11-22 14:20

    Having tried everything, I finally came up with a solution.

    Just place the following in your template:

    {{currentDirective.attr = parentDirective.attr; ''}}

    It just writes the parent scope attribute/variable you want to access to the current scope.

    Also notice the ; '' at the end of the statement, it's to make sure there's no output in your template. (Angular evaluates every statement, but only outputs the last one).

    It's a bit hacky, but after a few hours of trial and error, it does the job.

提交回复
热议问题