1

I am building an application in which user can select a word from the article displayed on a webpage, I save that word in the database for later use.

Which event can I use so that I can get which word the user has selected. I am using AngularJS. Below is a code which I found in jQuery, how can I write equivalent AngularJS code

$(document).ready(function() {

    var p = $('p');
    p.css({ cursor: 'pointer' });

    p.dblclick(function(e) {
        var range = window.getSelection() || document.getSelection() || document.selection.createRange();
        var word = $.trim(range.toString());
        if(word != '') {
            alert(word);
        }
        range.collapse();
        e.stopPropagation();
    });

});
1
  • 1
    You may want to look at how to write directives and well events are all the same, angular is just another javascript framework. Commented Nov 2, 2014 at 18:36

1 Answer 1

1

You just need to wrap it inside a directive:

app.directive('p', function() {
  return {
    restrict: 'E',
    link: function(scope, element, attrs) {
      element.css({ cursor: 'pointer' });
      element.on('dblclick', function(e) {
        var range = window.getSelection() || document.getSelection() || document.selection.createRange();
        var word = range.toString().trim();
        if(word !== '') {
          console.log(word);
        }
        e.stopPropagation();
      });      
    }
  }
});
Sign up to request clarification or add additional context in comments.

Comments

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.