2

I'm using AngularJS and at the moment I only manage to highlight a single word in a text. But I want to highlight multiple words using different colors.

Using this response from network call:

{
    "text" : "This is a long wall of text of which contains multiple words that I want to highlight using multiple colors",
    "word1" : "wall",
    "word2" : "words",
    "word3" : "colors"
}

I want to display the text like this:

results

2
  • 2
    Can you change the data structure or is an external endpoint? If you could change or adapt the data structure, you can do something like this JsFiddle that I've just created... Commented Feb 21, 2017 at 3:16
  • 1
    @The.Bear I may request for change but I'm not sure if it's possible. Anyway, thanks for your example. I will tinker around based on it and see if I can find a way. Really appreciate your time for helping me! Commented Feb 21, 2017 at 3:52

2 Answers 2

2

here is other way of doing bear's work fiddle!

function myCtrl($scope) {
$scope.item = {
text: "I am a Bear in the forest. We are two bears climbing a tree.",
search: [{
  text: "Bear",
  color: "brown"
}, {
  text: "forest",
  color: "green"
}, {
  text: "tree",
  color: "orange"
}]
};
}

var app = angular.module('myApp', ['highlightDirective']);
app.controller('myCtrl', ['$scope', myCtrl]);

function highlight() {

var directive = {
restrict: 'E',
scope: {
  text: '=',
  search: '='
},
link: function link(scope, $element, attr) {

var text = scope.text;
var search = scope.search;

for (var i = 0; i < search.length; i++) {
  var s = search[i];
  var html = '<span class="highlightText ' + s.color + '">$&</span>';

  text = text.replace(new RegExp("\\b" + s.text + "\\b"), html);
}

    $element.html(text);
    }
  };
  return directive;
}

var highlightDirectiveModule = angular.module("highlightDirective", []);
highlightDirectiveModule.directive('highlight', highlight);
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a way of doing it while keeping your data structure.

angular.module('myApp', [])
  .controller('myCtrl', function($scope) {
    $scope.response = {
      "text": "This is a long wall of text of which contains multiple words that I want to highlight using multiple colors",
      "word1": "wall",
      "word2": "words",
      "word3": "colors"
    };
  })
  .directive('highlight', function() {
    return {
      restrict: 'E',
      scope: {
        data: '='
      },
      link: function(scope, element, attrs) {
        let text = scope.data.text;
        delete scope.data.text;
        let words = Object.values(scope.data);

        element.html(text.split(" ").map(function(w) {
          let index = words.indexOf(w);
          return index !== -1 ? '<span class="word' + (index + 1) + '">' + w + '</span>' : w;
        }).join(" "));
      }
    };
  });
.word1 {
  background-color: yellow;
}

.word2 {
  background-color: green;
}

.word3 {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <highlight data="response"></highlight>
</div>

2 Comments

is there anyway I can set the color manually using CSS. eg .word1 { background-color: yellow; } .word2 { background-color: green; } .word3{ background-color: blue; }
I ended up changing the response format and modified code example provided by @The.Bear but this actually answer this question. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.