How to filter a Text in a div with JS angular

Asked

Viewed 907 times

1

The problem is to assemble a filter in Angularjs to capture words, querys, inside a text, for example as if to search a word in a craft.

<div ng-app="" ng-controller="namesController">

<p><input type="text" ng-model="test"></p>

<div ng-????="filter:test>
   <p> {{ texto }} </p>
</div>


</div>
  • But where do these words come from that you want to filter? From a ng-init? - doc.

3 answers

1

Use the angular filter itself

HTML

<div ng-app="myApp" ng-controller="myCtrl">
    <label>Filtrar: <input ng-model="searchText"></label>
    <div ng-repeat="user in users | filter:searchText">
      <p>
      Nome : {{user.nome}} -
      Idade : {{user.idade}}
      </p>
    </div>
</div>

Javascript

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.users = [];
    $scope.users.push({'nome':"Francisco", 'idade': 94});
    $scope.users.push({'nome':"Raimunda", 'idade': 85});
    $scope.users.push({'nome':"Francinaldo", 'idade': 77});
    $scope.users.push({'nome':"Tereza", 'idade': 87});
});

0

I also could not leave the search 'insensitive' with Angular, Dae made by Jquery base with Source and using javascript as follows:

$(function(){
                $("#txtBusca").keyup(function(){
                        var texto = $(this).val().toUpperCase();

                        $(".ulItens li").css("display", "block");
                        $(".ulItens li").each(function(){
                                if($(this).text().toUpperCase().indexOf(texto) < 0)
                                   $(this).css("display", "none");
                        });
                });
        });

.ulItens -> with the class applied to all tags <ul> and with toUpperCase I can search without the default case sensitive.

  • But I saw here that you found the answer on this other link: http://answall.com/questions/48972/problema-com-uppercase-e-lowercase-no-angularjs Thanks!! : D

0

Guys solved the problem with the code below:

Filter PHP list with Angularjs

See here the code in the plunker

I happen to be having problems with words that are capitalized for example, a word that is "Protocol" and I type "protocol", the filter does not find it.

app js.

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
    $scope.model = {
        filter: ''
    };
});

app.directive('myHtmlFilter', [function() {
    return {
        restrict: 'A',
        scope: {
          filter: '=myHtmlFilter',
          element: '@'
        },
        link: function(scope, elem, attrs) {
          scope.$watch('filter', function(newval, oldval) {
            elem
              .find('ul>li')
                  .hide()
                  .find(scope.element)
                  .filter(':contains("'+scope.filter+'")')
               .parent()
                  .show();
          })
        }
    }
}]);

Index.html

<input type="text" ng-model="model.filter" />

<div my-html-filter="model.filter" element="h2">
  <ul>
    <li id="1">
        <h2>Protocolo</h2>
        <p>The Message...</p>
    </li>
    <li id="2">
        <h2>My second Post</h2>
        <p>The Message...</p>
    </li>
    <li id="3">
        <h2>My third Post</h2>
        <p>The Message...</p>
    </li>
  </ul>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.