Inserting text at Current Cursor position in Textarea Using AngularJs Ng-Click

后端 未结 1 928
忘了有多久
忘了有多久 2021-01-23 02:33

We are trying to implement like below requirement. Click of a button some text will be generated and it will be added into textarea. Button is using Ng-Onclick.

Button C

相关标签:
1条回答
  • 2021-01-23 02:56

    Here is the hero which my issues was resolved. Who ever it is really great that he solved my problem.

    and What exactly done is I created directive element as mentioned in this link and called that directive in my view .

    Boom it worked.

    http://plnkr.co/edit/Xx1SHwQI2t6ji31COneO?p=preview

    app.directive('myText', ['$rootScope', function($rootScope) {
        return {
            link: function(scope, element, attrs) {
                $rootScope.$on('add', function(e, val) {
                    var domElement = element[0];
    
                    if (document.selection) {
                        domElement.focus();
                        var sel = document.selection.createRange();
                        sel.text = val;
                        domElement.focus();
                    } else if (domElement.selectionStart || domElement.selectionStart === 0) {
                        var startPos = domElement.selectionStart;
                        var endPos = domElement.selectionEnd;
                        var scrollTop = domElement.scrollTop;
                        domElement.value = domElement.value.substring(0, startPos) + val + domElement.value.substring(endPos, domElement.value.length);
                        domElement.focus();
                        domElement.selectionStart = startPos + val.length;
                        domElement.selectionEnd = startPos + val.length;
                        domElement.scrollTop = scrollTop;
                    } else {
                        domElement.value += val;
                        domElement.focus();
                    }
                });
            }
        }
    }]);
    

    View Calling like this,

    directive name is , myText. so the code will be like in this view side.

     <textarea my-text=""> 
    
    0 讨论(0)
提交回复
热议问题