0

I am trying to make a text highlight using a search keyword. I did my best. But I couldn't get a result of what I required.

Now I don't have any idea to proceed further to complete my requirement. any one give the correct suggestion or help me to complete the highlight by search key word functionality?

Actually my search input sits in the header of the page. ( global ) from I would like to highlight the text in the page by keyword entries.

here is my code :

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

app.controller('headerController', function ($scope) {

  var replacer = function(match, item) {
            return '<span class="highlight">'+match+'</span>';
    }

  var tokenize = function(keywords) {
            keywords = keywords.replace(new RegExp(',$','g'), '').split(',');
            var i;
            var l = keywords.length;
            for (i=0;i<l;i++) {
                keywords[i] = '\\W'+keywords[i].replace(new RegExp('^ | $','g'), '')+'\\W';
            }
            return keywords;
        }

  $scope.search = {value:''}

  $scope.$watch('search.value', function(newval, oldval) {

    if(!newval) {
      return false;
    }

    var tokenized   = tokenize(newval); 
    var regex       = new RegExp(tokenized.join('|'), 'gmi');

   console.log( tokenized );

  })


})

app.controller('controller1', function($scope) {

  $scope.para1 = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.';

});

app.controller('controller2', function($scope) {

  $scope.para2 = 'It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';

});

app.controller('controller3', function($scope) {

  $scope.para3 = 'The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.';

});

Live Demo

teobais
  • 2,820
  • 1
  • 24
  • 36
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • Maybe this will help you http://stackoverflow.com/questions/15519713/highlighting-a-filtered-result-in-angularjs – Dmitri Pavlutin Dec 28 '15 at 10:50
  • I guess they using a plug-in, and UI - I din't use one of them here. – 3gwebtrain Dec 28 '15 at 11:00
  • 1
    http://plnkr.co/edit/Py0cBMV3j89cEagWr52k?p=preview your plunker updated. Basically its using a technic described here: http://stackoverflow.com/a/27798600/4527675 – ChrisY Dec 28 '15 at 13:05

0 Answers0