9

I'm building click to edit directives, but have issues understanding how to access parent scope in an isolated directive.

Example: http://jsfiddle.net/ADukg/3591/

scope: {},

It works if I "unisolate" the scope removing scope: {}; but need the isolated scope.

UPDATE:

Done it adding

controller: 'FormCtrl',

To the directive. See http://jsfiddle.net/ADukg/3601/

aalimovs
  • 175
  • 1
  • 2
  • 10
  • possible duplicate of [How to access parent scope from within a custom directive \*with own scope\* in AngularJS?](http://stackoverflow.com/questions/17900201/how-to-access-parent-scope-from-within-a-custom-directive-with-own-scope-in-an) – thorn0 Apr 30 '14 at 22:41

2 Answers2

18

You could use the $parent property on the isolate scope to get direct access to the parent scope, but normally you'll want to use attributes to specify which parent scope properties the directive needs to do its work.

If you need to change the parent scope properties in the directive, bind with = (two-way objects). If you only need the string values of the parent scope properties in the directive, bind with @ (one-way strings).

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
2

The given solution won't work if you pass an attribute with primitive type like 'string', 'long' .... etc The two-way binging works only with object.

Every scope object contains a special property called $parent which refers to its parent scope. The isolated scope also has a $parent property. But it refers to the enclosing controller/directive's scope.

To make it work with primitive attributes: you could bind your directive template to a controller. This will expose your directive to its parent and you can access by $parent.

Second solution is to not make an isolate scope (but i don't think it's your goal).