Highlight the search text - angular 2

前端 未结 7 863
心在旅途
心在旅途 2020-12-01 05:22

A messenger displays the search results based on the input given by the user. Need to highlight the word that is been searched, while displaying the result. These are the h

7条回答
  •  温柔的废话
    2020-12-01 05:55

    To expand on Kamal's answer,

    The value coming into the transform method, could be a number, perhaps a cast to string String(value) would be safe thing to do.

    transform(value: string, args: string): any {
        if (args && value) {
            value = String(value); // make sure its a string
            let startIndex = value.toLowerCase().indexOf(args.toLowerCase());
            if (startIndex != -1) {
                let endLength = args.length;
                let matchingString = value.substr(startIndex, endLength);
                return value.replace(matchingString, "" + matchingString + "");
            }
    
        }
        return value;
    }
    

提交回复
热议问题