Why does AngularJS ng-repeat filter for search term not work?

reubonwry

I'm trying to make a table that filters to only show rows that contain whatever string is in the search box. I have a simple example that I'm just trying to get to work based off the w3schools tutorial: https://www.w3schools.com/angular/tryit.asp?filename=try_ng_tables_css

This is my filter <tr ng-repeat="x in names | filter:searchKeyword">

And it looks pretty similar to the example given in the API reference: <tr ng-repeat="friend in friends | filter:searchText"> https://docs.angularjs.org/api/ng/filter/filter

The problem is that nothing happens when I type stuff in the search box. I expect the table to dynamically change as the search term changes. What am I missing?

Here is the code I have:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
    <label>Search: <input type="text" ng-model="searchKeyword"></label>
    <div ng-app="myApp" ng-controller="customersCtrl"> 
        <table>
          <tr ng-repeat="x in names | filter:searchKeyword">
            <td>{{ x.Name }}</td>
            <td>{{ x.Country }}</td>
            <td>{{ x.City }} </td>
          </tr>
        </table>
    </div>

    <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope) {
        $scope.names = [
        {"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"}, 
        {"Name":"Ana Trujillo Emparedados y helados","City":"México D.F.","Country":"Mexico"}, 
        {"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"}, 
        {"Name":"Around the Horn","City":"London","Country":"UK"}, 
        {"Name":"B's Beverages","City":"London","Country":"UK"}, 
        {"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"}, 
        {"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"}, 
        {"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"}, 
        {"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"}, 
        {"Name":"Bon app'","City":"Marseille","Country":"France"}, 
        {"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"}, 
        {"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"}, 
        {"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"}, 
        {"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"}, 
        {"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"}
        ];
    });
    </script>
</body>
</html>
Dan

You just need to put your input box inside your app :) Also, DOM filters suck (inject $filter into your controller instead). See why: https://toddmotto.com/use-controller-filters-to-prevent-digest-performance-issues/

(Just putting my comment in answer form as requested.)

    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope) {
        $scope.names = [
        {"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"}, 
        {"Name":"Ana Trujillo Emparedados y helados","City":"México D.F.","Country":"Mexico"}, 
        {"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"}, 
        {"Name":"Around the Horn","City":"London","Country":"UK"}, 
        {"Name":"B's Beverages","City":"London","Country":"UK"}, 
        {"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"}, 
        {"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"}, 
        {"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"}, 
        {"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"}, 
        {"Name":"Bon app'","City":"Marseille","Country":"France"}, 
        {"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"}, 
        {"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"}, 
        {"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"}, 
        {"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"}, 
        {"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"}
        ];
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl"> 
    <label>Search: <input type="text" ng-model="searchKeyword"></label>
    <table>
      <tr ng-repeat="x in names | filter:searchKeyword">
        <td>{{ x.Name }}</td>
        <td>{{ x.Country }}</td>
        <td>{{ x.City }} </td>
      </tr>
    </table>
</div>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

AngularJS: Why does this delegated ng-repeat not work inside a transclusion?

分類Dev

Why does the ng-repeat derective not work?

分類Dev

How does filter work in AngularJS?

分類Dev

AngularJS: Can I use a filter to chunk an array in ng-repeat?

分類Dev

AngularJS ng-repeat filter, function over object property

分類Dev

Angularjs Nested ng-repeat with expression used in filter

分類Dev

AngularJS filter to ng-repeat, exclude elements, if they in second array

分類Dev

AngularJS filter to ng-repeat, exclude elements, if they in second array

分類Dev

Why does ng-mouseover not work with ng-if

分類Dev

angular filter for the ng-repeat

分類Dev

AngularJS「垂直」ng-repeat

分類Dev

AngularJS IF ELSE with ng-repeat

分類Dev

Custom search filter in angularjs

分類Dev

Why does a slight change in the search term slow down the query so much?

分類Dev

ng-repeat + ng-change - AngularJs

分類Dev

ng-repeat + ng-change-AngularJs

分類Dev

angularjs ng-repeat inside ng-if

分類Dev

Why AngularJS ng-bind does not update the view

分類Dev

Why angularjs(ng-repeat) allowing to add duplicate record at first time if the input array values are integer types

分類Dev

mod_filter: Why does a SUBSTITUTE not work for certain URLs?

分類Dev

Function is not called because of ng-repeat filter

分類Dev

ng-repeat add items(AngularJs 1.2.26)

分類Dev

AngularJs:ng-repeat = "item in arrayValue"

分類Dev

How to display highcharts in ng-repeat angularjs

分類Dev

AngularJs ng-repeat make new <div>

分類Dev

AngularJS do not sort in ng-repeat

分類Dev

AngularJs。ng-repeatの問題

分類Dev

AngularJs ng-repeat in group class

分類Dev

Show ng-repeat selectively in AngularJs

Related 関連記事

ホットタグ

アーカイブ