Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing parent scope in isolated directive

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/

like image 484
aalimovs Avatar asked Jul 24 '13 18:07

aalimovs


People also ask

How do I get parent scope in directive?

You can still access the parent scope using $parent , but this is not normally recommended. Instead, you should specify which parent scope properties (and/or function) the directive needs via additional attributes on the same element where the directive is used, using the = , @ , and & notation.

How do you access child controller scope in parent controller?

In a nutshell: You cannot access child scopes from a parent scope. Your solutions: Define properties in parents and access them from children (read the link above) Use a service to share state.

What is scope parent?

Angular scopes include a variable called $parent (i.e. $scope. $parent ) that refer to the parent scope of a controller. If a controller is at the root of the application, the parent would be the root scope ( $rootScope ). Child controllers can therefore modify the parent scope since they access to it.

How do you access the directive variable in a controller?

You just create a myVar variable in your controller and pass it to the directive using my-var attribute. Since you are using two way binding, any changes made to myVar by the directive are available in your controller.


1 Answers

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).

like image 90
Mark Rajcok Avatar answered Oct 03 '22 23:10

Mark Rajcok