1

I try to pass a value from parent scope to my directive. To its isolated scope. It works ok if I pass values as argument to my directive. But id does not work for parent scope.

Here is how I use it:

<div ng-init="event='hello'">  <!-- this is parent scope -->

    <input31></input31>

</div>

Here is the directive itself.

directives.directive("input31", [function() {    
    return {
        restrict: 'E',
        template:"<div> <input type='text' ng-model='data' /> {{data}} </div>",
        replace: true,
        scope : { //defining isolated scope
            event: "=event"  // pushing value to isolated directive scope
        }
    }

}]);

In the end I'm expecting to have event var filled by 'hello' value but I have null

update

I thought that it's possible to select/filter what to use from parent scope, kind of filter. Like if in parent scope I have a,b,c then specifying 'scope {a: '=a'}' I could transfer only 'a' from parent scope. But as I understand this not the way to go. this work only if passing values as directive arguments (?)

3 Answers 3

4

Using an isolate scope binding you'll need to pass the value in as an attribute on the directive.

<input31 event=event></input31>

This can be a plus from a documentation/maintenance standpoint.

But, if you really don't want to pass it in as an attribute, but otherwise want an isolate scope, then instead of using a binding you can copy the value off the parent's scope inside the directive's link function.

link: function ($scope) {
    $scope.event = $scope.$parent.event;
}

If you do this then you don't need to pass it in via the isolate scope, so the directive's scope can look like this:

scope : { 
        },
Sign up to request clarification or add additional context in comments.

2 Comments

I thought that it's possible to select/filter what to use from parent scope, kind of filter. Like if in parent scope I have a,b,c then specifying 'scope {a: '=a'}' I could transfer only 'a' from parent scope. But as I understand this not the way to go.
You're right that you can do exactly that. But you have to both specify it in the scope and specify the mapping on the directive. Those two pieces work together. Here's a good couple minute overview:egghead.io/lessons/angularjs-understanding-isolate-scope
0

You could use the existing scope from directive's DOM element. You can do do this by either:

  1. Setting scope as false
  2. Not setting scope at all (by default scope is set to false).

Please see this jsFiddle for an example.

Your directive could then be defined like this:

app.directive('input31', function () {
    return {
        restrict: 'E',
        template:"<div> <input type='text' ng-model='event' /> {{event}} </div>",
        replace: true,
        scope : false // or omit this line
    };
});

If you require an isolated scope then you have to specify what values to bind from the parent scope. You can do this in two ways:

  1. Pass specific attributes to your directive's isolated scope e.g. <input31 event="event">
  2. Reference the parent scope directly in your directive. e.g. $scope.$parent.event

1 Comment

Updated my question. (That what you're pointing I've already knew)
0

I've put an example of what I have been asking.

https://github.com/Sergey80/angularjs-examples/blob/master/public/examples/directive_isolate_scope/sample6_at_sign.html

There is way to use parent scope from within Perent scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.