0

how to access the search model inside your header directive using angularJS

     <div header showprofile="false" >
        <div class="searchView col-xs-12">

                <input type="text" placeholder="Search" ng-model="search" />         
        </div>
    </div>
    <div class="divtop">
        <!-- <input type="text" placeholder="Search" ng-model="search" /> -->
        <div class="it-content col-xs-12 col-sm-12 col-md-12">
            <div ng-repeat="it in itBatch.its | filter: search" class="visit-card">

</div>
</div>
</div>
2
  • I think what you're asking is how to access the search model inside your header directive, is that right? Commented Apr 6, 2016 at 11:08
  • s that is what am trying for Commented Apr 6, 2016 at 11:12

1 Answer 1

1

You can pass the model to the directive like this -

<div header showprofile="false" search="search">
    <div class="searchView col-xs-12">
        <input type="text" placeholder="Search" ng-model="search" />         
    </div>
</div>

Update your directive to accept it

.directive('header', function() {
  function link(scope, element, attrs) {
    scope.$watch(attrs.search, function(value) {
      console.log(value);
    });
  }
  return {
    restrict: 'EA',
    scope: {
      showprofile: '=',
      search: '=',
    },
    link: link
  };
});

UPDATE Here is a fiddle.

Hope that helps.

Sign up to request clarification or add additional context in comments.

4 Comments

can you please post your directive code and explain what you're trying to do?
Am trying to give search filter for my page in the header
Please check my updated answer, it's working fine for me.. jsfiddle.net/yd7gddbu/1
@MadhumithaReddy what?

Your Answer

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