0

I am using the inbuilt search filter of angular js like this-

<!--header starts-->
<input class="form-control fs-mini search" ng-model="search.name" type="text" placeholder="&#xe80a Search">   
<!--header ends-->
<!--content area-->
<div  ng-repeat="user in users  | filter:search">
                {{ user.name }}
                            {{user.l}}


                            {{user.time}}

            </div>
<!--content area ends-->

Now i removed the header template codes and created the header directive.

<!--header starts-->
 <div site-header>
   </div>
<!--header ends-->
<!--content area-->
<div  ng-repeat="user in users  | filter:search">
                {{ user.name }}
                            {{user.l}}


                            {{user.time}}

            </div>
<!--content area ends-->

site-header.js:

'use strict';

    angular.module('myApp')
        .directive('siteHeader', function () {
            return {
                templateUrl: 'views/header-view.html',
                scope: {

                },
                restrict: 'A',
                controller: [ '$scope', '$rootScope', '$location', function($scope, $rootScope, $location) {
                            console.log($scope.data);
                }],
                link: function postLink(scope, element, attrs) {

                }
            };
        });

header-view.html

<input class="form-control fs-mini search" ng-model="search.name" type="text" placeholder="&#xe80a Search">

All html templates is loading correctly. but the search filter is not working. I dont understand how to bind search.name to the directive in order to work. I tried like this-

<div site-header="search.name">
   </div>

but how to access this data in directive and bind it to ng-model?

1
  • Write a custom filter in the controller like below and check whether it's working or not. $scope.filterByName = function(item) { if(!$scope.search) return true; var itemName = angular.lowercase(item.name); return (itemName.indexOf(searchItem) !== -1); }; Commented May 14, 2015 at 7:04

2 Answers 2

1

Change your directive definition:

app.directive('myDirective', function(){
      return {
        restrict: 'A',
        template: [
            '<input class="form-control fs-mini search" ng-model="search.name" type="text" placeholder="&#xe80a Search"/>'
          ].join(''),
        scope: true
      }
    });

here is a demo:http://plnkr.co/edit/JNCjzs?p=preview

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

2 Comments

I changed it slightly
It works. can you provide me the resource to read about this concept.Its hard to grab. Specially the use of ng-init="search.name = 'type in filter'"
0

Directives get their own scopes in angular. Scopes prototypically inherit from their parent scopes, which can be confusing and takes some time and knowledge of js inheritance to get your head around. If you can't be bothered with that, a quick solution to your problem would be to use an object to store the search string and instantiate it in the parent scope, which will make it available in both scopes. I think all you need is to put the line:

`var search = {};`

in your controller.

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.