简体   繁体   中英

how to search and highlight in angular js?

I have a div in which I have some contend in that .I need to search text from that div and highlight the word (using css class).using next it search next word occurrence and previous occurrence. I am able to make in jquery as example in fiddle http://jsfiddle.net/3MVNj/5/ can you please suggest how I will achieve this in angular js ? plunker http://plnkr.co/edit/V805fEjJAUbAZIuafJcW?p=preview

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <link data-require="bootstrap-css@3.x" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
    <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
    <script data-require="ui-bootstrap@0.10.0" data-semver="0.10.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>
<style>
  .highlightedText {
    background: yellow;
}
</style>
  <body>
    <div ng-controller="contr">
      <input type="text" ng-model="searchtext" />
      <button ng-click="searchFirst()">FirstText</button>
      <button ng-click="nextSearch()">NextSearch</button>
      <button ng-click="preSearch()">PreviousSearch</button>
      <div id="contend">hello I need to search text from the contend

        .On click of search it search the first word.
      </div>
    </div>
    <script>
    var app=angular.module('app',[]);
    app.controller("contr",function($scope){
      $scope.searchFirst=function(){
        alert('search First text')

      }
       $scope.nextSearch=function(){
                alert('nextSearch')

      }
       $scope.preSearch=function(){
                        alert('preSearch')

      }
    });
  </script>
  </body>

</html>

is there any way to search filter ?

you can use your old search algorithm, as an example: http://plnkr.co/edit/As9iyCVtMMNH7lyprJA9?p=preview

what is added is

<button ng-click="searchFirst(searchtext)">FirstText</button>

example search, forward only (prev implementation not AngularJS related)(see link for running example):

Try searching "sear"... have tested on that word only... =)

app.controller("contr",function($scope){

  var searcher = new Searcher();

  var div = angular.element(document.getElementById('content'));

  $scope.searchFirst=function(str){
    searcher.setOffset(0);
    searcher.setContent(div.text());
    div.html(searcher.search(str));
  }

  $scope.nextSearch=function(){
    div.html(searcher.search());
  }

   $scope.preSearch=function(){
     // you implement
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM