0

I am very much new in AngularJS and due to which I am facing an issue and not able to understand the exact problem. The code which I tried is worked in normal javascript code but now I want to use custom service (factory function). Actually, I have a textarea where user can input their text and then they can do the formating by selecting any of the text. (discovertheweb.in/woweditor - this is existing site which I have created now I want to apply angular in this code). Now, I have created a custom service to check whether the user select any content or not. I have used the below code in my custom services and try to get the selection start, end point so that I can get the selected text from the textarea field. But, the problem is that I am getting 0 value for both selection start and end point. When i used the same code inside directive it works but try to get the value through service it is showing 0 for both. Please find the below code and let me know the code which I missed out here.

Custom Service:

(function(){
"use strict";
var wsApp = angular.module("WorkApp");
wsApp.factory("InputCheckService", function(){
    var defaultText = document.getElementById("input-text-area");                   
    var selStart = defaultText.selectionStart;
    var selEnd =  defaultText.selectionEnd;
    var selectedText;
    if(selStart != selEnd){
        selectedText = defaultText.value.substring(selStart, selEnd);                            
    }else{
        selectedText = null;
    }
    return {            
        selStart: selStart,
        defaultText: defaultText,
        selEnd: selEnd,
        selectedText: selectedText
    };
});    

}());

The directive where I called this services. I already included the service inside the main controller in different file.

(function(){
"use strict";
var wsApp = angular.module("WorkApp");
wsApp.directive("generalDirective", generalDirective);
function generalDirective(){
    return {
        scope: true,
        controller:function($scope, InputCheckService){
            $scope.collapsed = false;
            $scope.CollpasePanel = function(){
                $scope.collapsed = !$scope.collapsed;
            };
            $scope.updatePara = function(){
                alert(InputCheckService.defaultText+"Selection start: "+InputCheckService.selStart+" Selection End: "+ InputCheckService);
                /**
                 * defaultText: defaultText,
                    selStart: selStart,
                    selEnd: selEnd,
                    selectedText: selectedText
                 */
            }
        },
        templateUrl: 'directive/general-directive.html'
    };
}    

}());

If you need any more details, please let me know.

Thanks in advance for your time and suggestion.

Regards, Robin

Robindra Singha
  • 141
  • 1
  • 2
  • 12

1 Answers1

1

You should not use service to manipulate DOM element. You should manipulate DOM only at directives. Your problem is you DONT have anywhere to listen to TEXTAREA SELECTION EVENT and your service will not update the data inside. I have created a fiddle for your problem. The watchSelection directive is based on this answer from stackoverflow. Something you should notice :

  1. I use service only to store data. Something like selStart, selEnd or paragraphContent and provide some API to retrieve the data

    .factory("InputCheckService", function () {
            return {
                   setSelStart: function (start) {
                        selStart = start;
                   },
                   .....
            },
    });
    
  2. On the watchSelection directive, you watch for the mouseup event and will perform update the service so that it will store value you need and later you can retrieve it from other directives or controllers.

    elem.on('mouseup', function () {
    
        var start = elem[0].selectionStart;
    
        //store it to the service
        InputCheckService.setSelStart(start);
    });
    
  3. In your generalDirective directive you can get value from your service and angular will auto update the view for you.

Hope it helps.

Community
  • 1
  • 1
themyth92
  • 1,743
  • 2
  • 17
  • 23
  • Thank you so much for your help. Now, I am able to get all the values and do the require action through the said process. Thank you again. – Robindra Singha Apr 29 '15 at 13:49