5

I'm trying to make two ways of filtering (via clicking on letters or via typing in input field).

<body ng-controller="MainController">
    <ul search-list=".letter" model="search">
        <li class="letter" ng-repeat="letter in alphabet.letter">{{ letter }}</li>
    </ul>

    <div class="container" ng-controller="CountriesController">

        <input id="q" type="text" ng-model="search " />
        <ul>
            <li ng-repeat="country in countries.country | filter:search:startsWith">{{ country }}</li>
        </ul>

    </div>
</body>

and js:

var app = angular.module('demo', []);
var controllers = {};
var alphabet = "abcdefghijklmnopqrstuvwxyz".split("");



app.directive('searchList', function() {
    return {
        scope: {
            searchModel: '=model'
        },
        link: function(scope, element, attrs) {
            element.on('click', attrs.searchList, function() {
                scope.searchModel = $(this).text();
                scope.$apply();
            });
        }
    };
});

controllers.MainController = function($scope) {
    $scope.setTerm = function(letter) {
        $scope.search = letter;
    };
    $scope.alphabet = {
      letter: alphabet
    }
};
controllers.CountriesController = function($scope){

  $scope.countries = {
    country:['Ukraine','Urugvai','Russia','Romania','Rome','Argentina','Britain']
  }
  $scope.startsWith = function (actual, expected) {
    var lowerStr = (actual + "").toLowerCase();
    return lowerStr.indexOf(expected.toLowerCase()) === 0;
  }
};



app.controller(controllers);

On first look everything is working fine, but it is so only on first look...

When at first I click on any letter - filter works good. When then I'm typing in input field - filter works good. But after I typed in input field or delete text from there via 'backspace' button - filter by clicking on letters stops working...

Why it works like this and how I can fix this issue? Here is my DEMO

Thanx in advance!

Johnny Juarez
  • 248
  • 6
  • 18
  • 3
    The problem is scope inheritance. The moment you actually type into the input field, the scope of `CountriesController` will have it's own value for `search` and no longer inherit it from `MainController`. This could be resolved if you follow the [dot-rule](http://stackoverflow.com/q/17606936/697154). – Yoshi Jun 30 '15 at 10:58

2 Answers2

2

The problem fount for using two controler's. If you remove CountriesController , then everything working good.

The Search values was confused with which controller using that scope object.I think you don't need implement two controller for this scenario

See the working demo if you remove CountriesController.

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • Thank You for your explanation. But may be is better to add '$parent.' to 'ng-model="search"', just in case if on ather page I'll need to use only CountriesController without filtering on click? – Johnny Juarez Jun 30 '15 at 11:40
1

change this part of html to this it works fine (ng-model="$parent.search ")

   <div class="container" ng-controller="CountriesController">

        <input id="q" type="text" ng-model="$parent.search " />
        <ul>
            <li ng-repeat="country in countries.country | filter:search:startsWith">{{ country }}</li>
        </ul>

    </div>