2017-02-21 1 views
1

저는 AngularJS를 사용하고 있으며 텍스트의 한 단어 만 강조 표시합니다. 하지만 여러 색상을 사용하여 여러 단어를 강조하고 싶습니다.AngularJS 필터를 사용하여 여러 단어 강조 표시

네트워크 호출에서이 응답 사용 :

{ 
    "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" 
} 

내가 이런 식으로 텍스트를 표시하려면 :

results

+2

당신은 데이터 구조를 변경하거나 외부 엔드 포인트는? 데이터 구조를 변경하거나 적용 할 수 있다면 [JsFiddle은 방금 만든 ...] (http://jsfiddle.net/j28kmq42/14/) –

+1

@ The.Bear 변경 요청하지만 가능하면 잘 모르겠습니다. 어쨌든, 당신의 예를 들어 주셔서 감사합니다. 나는 그것을 바탕으로 주위를 고치고 길을 찾을 수 있는지 알아볼 것입니다. 나를 도와 주셔서 정말 고마워요! –

답변

1

데이터 구조를 유지하면서이를 수행하는 방법입니다.

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>

+0

어쨌든 CSS를 사용하여 수동으로 색상을 설정할 수 있습니다. 예.'.word1 { background-color : yellow; } .word2 { background-color : green; } .word3 { background-color : blue; }' –

+0

@ The.Bear에서 제공 한 응답 형식 및 수정 된 코드 예제를 변경했지만 결국이 질문에 대답합니다. :) –

1

여기 곰의 작품 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); 
관련 문제