Getting scope value in child directive

user70192

I am using the Typeahead directive from the UI Bootstrap framework. I am using this directive inside of another directive. My outer directive is defined like this:

.directive('myDirective', function () {
  return {
    restrict:'E',
    transclude: true,
    scope: {
      showLinks: '=?',
      query: '='
    },
    templateUrl: '/directives/my-directive.html',
    controller: function ($scope) {
      if (angular.isUndefined($scope.showLinks)) {
        $scope.showLinks = true;
        console.log('show links? ' + $scope.showLinks);
      }
    }
  };
});

my-directive.html looks like this:

<div style="width:100%;">
    <span>{{showLinks}}</span> <!-- renders just fine -->
    <input type="text" autofocus autocomplete="off" class="form-control" ng-model="query"
           typeahead="option as option.Name for option in getLocation($viewValue)"
           typeahead-min-length="3" typeahead-template-url="location.html" />
    <script type="text/ng-template" id="location.html">
      {{showLinks}} <!-- Never renders -->
      <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
    </script>
</div>

I'm calling my directive like this:

<my-directive show-links="true" />

I need to use the value of showLinks inside of the template I'm using for the typeahead. For some reason though, it is not beings passed into the template. I believe it has something to do with scoping. I've come to this conclusion by binding the value into the UI as shown above. The value for showLinks appears just fine in my template. However, it does not appear in the template of the typeahead instance in my directive.

What am I doing wrong? I feel like I am so close. Yet, I've been working on this all day.

Thank you for any help you can provide!

Marian Ban

The typehead directive is creating its own isolated scope thus you can't access any properties which are not part of this scope. Basically you can access only these properties:

scope:{
    index:'=',
    match:'=',
    query:'='
}

but you can access the model property of the mach match.model so in your example you can add the showLinks variable as a argument to the getLocation method and map its value to option object:

<input type="text" autofocus autocomplete="off" class="form-control" ng-model="query"
           typeahead="option as option.Name for option in getLocation($viewValue, showLinks)"
           typeahead-min-length="3" typeahead-template-url="location.html" />

template

 <script type="text/ng-template" id="location.html">
      {{match.model.showLinks}} <!-- the show linLinks property is mapped inside getLocation method -->
      <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
    </script>

getLocation example:

$scope.getLocation = function(viewValue, showLinks)
{
   var locations = [];
   // code to fill locations

   // extend each location (option) object with showLinks data
   for (int i=0; i<locations.length; i++)
   {
        var location = locations[i];
        location.showLinks = showLinks;
   }   

   return locations;

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting scope value in child directive

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

From Dev

AngularJS - Getting an input value and passing it to parent directive scope

From Dev

Change a scope value in directive

From Dev

AngularJS : Child directive scope to inherit from parent directive isolated scope?

From Dev

AngularJS 1.4 - how to bind value between parent and child directive with isolated scope

From Dev

Assigning value to scope in directive not working

From Dev

AngularJs directive, scope with value and function

From Dev

Changing a scope value from a directive

From Dev

directive scope rewrite parent value

From Dev

AngularJs directive, scope with value and function

From Dev

Use a directive to display scope value and update scope

From Dev

In nested directive, why is my child directive getting the attributes of the parent directive?

From Dev

Wrong scope when using $compile for a child directive

From Dev

AngularJS accessing isolated scope in child directive

From Dev

Custom child directive accessing scope of parent

From Dev

Child directive template not updating after scope change

From Dev

Accessing parent "isolated" scope from child directive

From Dev

AngularJS accessing isolated scope in child directive

From Dev

Wrong scope when using $compile for a child directive

From Dev

Update the Parent Scope from Child Directive

From Dev

Child directive template not updating after scope change

From Dev

Directive - getting values from views -scope "@"

From Dev

why $scope variable inside directive is not getting updated?

From Dev

Sharing scope/data between directive controller and a child directive

From Dev

Access parent directive's controller scope variables in child directive

From Dev

Access parent directive's controller scope variables in child directive

Related Related

  1. 1

    Getting scope value in child directive

  2. 2

    Use, in child directive, value from parent directive and $scope

  3. 3

    Use, in child directive, value from parent directive and $scope

  4. 4

    Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

  5. 5

    AngularJS - Getting an input value and passing it to parent directive scope

  6. 6

    Change a scope value in directive

  7. 7

    AngularJS : Child directive scope to inherit from parent directive isolated scope?

  8. 8

    AngularJS 1.4 - how to bind value between parent and child directive with isolated scope

  9. 9

    Assigning value to scope in directive not working

  10. 10

    AngularJs directive, scope with value and function

  11. 11

    Changing a scope value from a directive

  12. 12

    directive scope rewrite parent value

  13. 13

    AngularJs directive, scope with value and function

  14. 14

    Use a directive to display scope value and update scope

  15. 15

    In nested directive, why is my child directive getting the attributes of the parent directive?

  16. 16

    Wrong scope when using $compile for a child directive

  17. 17

    AngularJS accessing isolated scope in child directive

  18. 18

    Custom child directive accessing scope of parent

  19. 19

    Child directive template not updating after scope change

  20. 20

    Accessing parent "isolated" scope from child directive

  21. 21

    AngularJS accessing isolated scope in child directive

  22. 22

    Wrong scope when using $compile for a child directive

  23. 23

    Update the Parent Scope from Child Directive

  24. 24

    Child directive template not updating after scope change

  25. 25

    Directive - getting values from views -scope "@"

  26. 26

    why $scope variable inside directive is not getting updated?

  27. 27

    Sharing scope/data between directive controller and a child directive

  28. 28

    Access parent directive's controller scope variables in child directive

  29. 29

    Access parent directive's controller scope variables in child directive

HotTag

Archive