1

I want to pass data generated by jQuery into controller of AngularJS. Is there any way possible to do this.

 <textarea  ng-click="showSelectedText(selection.text)" name="editor1" id="editor1" cols="118" rows="35">

jQuery to gather Data:

  $( "#editor1" ).select(function() {

                var selection = getSelected()
                if(selection)
                {

                    alert(selection);
                }

                });  

                function getSelected() {
                    if (window.getSelection) {
                        return window.getSelection();
                    }
                    else if (document.getSelection) {
                        return document.getSelection();
                    }
                    else {
                        var selection = document.selection && document.selection.createRange();
                        if (selection.text) {
                            return selection.text;
                        }
                        return false;
                    }
                    return false;
            }  

Controller of AngularJS:

 $scope.showSelectedText = function(fromUI) {

        alert("Text is : "+ fromUI);
    };
2
  • Do you only need to pass textarea's text in your controller ? Commented May 20, 2015 at 6:40
  • from text area I just need to pass the selected/highlighted text {NOT the Whole text} to the controller of AngularJs Commented May 20, 2015 at 6:45

1 Answer 1

2

You need to make few changes. Like give an id to the element where you've defined your controller, like

<div id="demoElement" ng-app='MyModule' ng-controller="MyController">
</div>

In your controller remain this function same as it is. ie

$scope.showSelectedText = function(fromUI) {

        alert("Text is : "+ fromUI);
    };

Now you could remove ng-click="showSelectedText(selection.text)" from the textarea and call angular's function from your jquery code by using below line.

angular.element(document.getElementById('demoElement')).scope().showSelectedText(jQueryObjectOfSelectedText);

You could call this function from your jQuery code where you're getting your selected text, like

  $( "#editor1" ).select(function() {

                    var selection = getSelected()
                    if(selection)
                    {
 angular.element(document.getElementById('demoElement')).scope().showSelectedText(selection);
                    }

                    });  

                    function getSelected() {
                        if (window.getSelection) {
                            return window.getSelection();
                        }
                        else if (document.getSelection) {
                            return document.getSelection();
                        }
                        else {
                            var selection = document.selection && document.selection.createRange();
                            if (selection.text) {
                                return selection.text;
                            }
                            return false;
                        }
                        return false;
                }  
Sign up to request clarification or add additional context in comments.

5 Comments

console shows this error "Uncaught TypeError: Cannot read property 'showSelectedText' of undefined :
Please make sure you're passing the same element in angular.element() where your controller has defined.
with error as "Uncaught TypeError: document.getElementById(...).scope is not a function"
Thankyou stackoverflow.com/users/4366287/vineet it really helped me to solve the problem

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.