Fourth
Fourth

Reputation: 9351

Bind to text highlighting

I am trying to bind a controller action to text that is highlighted in a text area, text input, or content-editable. Suppose that I have:

<input type="text" ng-model="name" placeholder="Enter Name">

With Angular 1.2.0, how can I watch for text that is highlighted inside the text box and display something on the page for the user?

Upvotes: 0

Views: 2414

Answers (3)

Ye Liu
Ye Liu

Reputation: 8986

You can create a directive to utilize selectionStart and selectionEnd properties of an input element to achieve what you want to accomplish, like the following:

JS:

directive('watchSelection', function() {
    return function(scope, elem) {
        elem.on('mouseup', function() {
            var start = elem[0].selectionStart;
            var end = elem[0].selectionEnd;
            scope.selected = elem[0].value.substring(start, end);
            scope.$apply();
        });
    }; 
});

HTML:

<input type="text" ng-model="name" placeholder="Enter Name" watch-selection>

http://plnkr.co/edit/4LLfWk110p8ruVjAWRNv

Upvotes: 1

Langdon
Langdon

Reputation: 20073

Here is a pretty rough implementation of a directive that uses $timeout. It could probably be improved by monitoring mouseup and keyup (or selection events if they exist).

http://jsfiddle.net/4XDR8/1/

HTML

<div ng-app="app" ng-controller="TestCtrl">
    <input type="text" placeholder="Enter Name" ng-get-selection="name">
    {{name}}
    <br/>
    <br/>here select all this text down here
</div>

JavaScript:

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

app.directive('ngGetSelection', function ($timeout) {
    var text = '';

    function getSelectedText() {
        var text = "";
        if (typeof window.getSelection != "undefined") {
            text = window.getSelection().toString();
        } else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
            text = document.selection.createRange().text;
        }
        return text;
    }

    return {
        restrict: 'A',
        scope: {
            ngGetSelection: '='
        },
        link: function (scope, element) {
            $timeout(function getSelection() {
                var newText = getSelectedText();

                if (text != newText) {
                    text = newText;
                    element.val(newText);
                    scope.ngGetSelection = newText;
                }

                $timeout(getSelection, 50);
            }, 50);

        }
    };
});

app.controller('TestCtrl', function ($scope) {
    $scope.name = '';
});

Upvotes: 3

MattDiamant
MattDiamant

Reputation: 8781

Here's how to grab the selected text from an input field:

http://jsfiddle.net/vREW8/

var input = document.getElementsByTagName('input')[0];
var selectedText = input.value.substring(input.selectionStart, input.selectionEnd);

You can use it with Anuglar.js any way that you want.

Upvotes: 0

Related Questions