最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs directive to Insert text at textarea caret - Stack Overflow

programmeradmin2浏览0评论

I have controller MyCtrl and directive myText. When a model in MyCtrl changes, I want to update the textarea of myText directive by inserting the text at current position / caret.

I tried to reuse the code from this Inserting a text where cursor is using Javascript/jquery

My Code:

Here is my HTML:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link href="style.css" rel="stylesheet" />
    <script data-semver="1.2.10" src=".2.10/angular.js" data-require="[email protected]"></script>
    <script src="app.js"></script>
  </head>

  <body>
    <div ng-controller="MyCtrl">
      <input ng-model="someInput">
      <button ng-click="add()">Add</button>
      <p ng-repeat="item in items">Created {{ item }}</p>  
    </div>

    <textarea my-text="">

    </textarea>

  </body>

</html>

Javascript:

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

app.controller('MyCtrl', function($scope, $rootScope) {
  $scope.items = [];

  $scope.add = function() {
    $scope.items.push($scope.someInput);
    $rootScope.$broadcast('add', $scope.someInput);
  }
});

app.directive('myText', ['$rootScope', function($rootScope) {
  return {
    link: function(scope, element, attrs) {
      $rootScope.$on('add', function(e, val) {
        console.log('on add');
        console.log(val);

        if (document.selection) {
          element.focus();
          var sel = document.selection.createRange();
          sel.text = val;
          element.focus();
        } else if (element.selectionStart || element.selectionStart === 0) {
          var startPos = element.selectionStart;
          var endPos = element.selectionEnd;
          var scrollTop = element.scrollTop;
          element.value = element.value.substring(0, startPos) + val + element.value.substring(endPos, element.value.length);
          element.focus();
          element.selectionStart = startPos + val.length;
          element.selectionEnd = startPos + val.length;
          element.scrollTop = scrollTop;
        } else {
          element.value += val;
          element.focus();
        }

      });
    }
  }
}])

It doesn't work now because element is not the DOM object. Here is the error:

TypeError: Object [object Object] has no method 'focus'

QUESTION: So my question is how to fix this? Or how to get the real DOM object from angular element object?

I have controller MyCtrl and directive myText. When a model in MyCtrl changes, I want to update the textarea of myText directive by inserting the text at current position / caret.

I tried to reuse the code from this Inserting a text where cursor is using Javascript/jquery

My Code: http://plnkr.co/edit/WfucIVbls2eekL8kUp7e

Here is my HTML:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link href="style.css" rel="stylesheet" />
    <script data-semver="1.2.10" src="http://code.angularjs.org/1.2.10/angular.js" data-require="[email protected]"></script>
    <script src="app.js"></script>
  </head>

  <body>
    <div ng-controller="MyCtrl">
      <input ng-model="someInput">
      <button ng-click="add()">Add</button>
      <p ng-repeat="item in items">Created {{ item }}</p>  
    </div>

    <textarea my-text="">

    </textarea>

  </body>

</html>

Javascript:

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

app.controller('MyCtrl', function($scope, $rootScope) {
  $scope.items = [];

  $scope.add = function() {
    $scope.items.push($scope.someInput);
    $rootScope.$broadcast('add', $scope.someInput);
  }
});

app.directive('myText', ['$rootScope', function($rootScope) {
  return {
    link: function(scope, element, attrs) {
      $rootScope.$on('add', function(e, val) {
        console.log('on add');
        console.log(val);

        if (document.selection) {
          element.focus();
          var sel = document.selection.createRange();
          sel.text = val;
          element.focus();
        } else if (element.selectionStart || element.selectionStart === 0) {
          var startPos = element.selectionStart;
          var endPos = element.selectionEnd;
          var scrollTop = element.scrollTop;
          element.value = element.value.substring(0, startPos) + val + element.value.substring(endPos, element.value.length);
          element.focus();
          element.selectionStart = startPos + val.length;
          element.selectionEnd = startPos + val.length;
          element.scrollTop = scrollTop;
        } else {
          element.value += val;
          element.focus();
        }

      });
    }
  }
}])

It doesn't work now because element is not the DOM object. Here is the error:

TypeError: Object [object Object] has no method 'focus'

QUESTION: So my question is how to fix this? Or how to get the real DOM object from angular element object?

Share Improve this question edited May 23, 2017 at 12:18 CommunityBot 11 silver badge asked Jan 28, 2014 at 9:01 HP.HP. 19.9k56 gold badges159 silver badges258 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

Try:

var domElement = element[0];

DEMO

Short anwser:

element[0]

would give you the actual DOM Element.

Long answer:

angular.element always provides a jqLite selector, which is similiar to a result set given by a jQuery selection. It's a set of HTML elements.

The link function of a method gets called with the following params: scope, element, attrs and ctrl.

element is given as a set of the one DOM Element the directive is attached to. So the first item is the actual HTML Element.

I changed your code so that it should(?) work

发布评论

评论列表(0)

  1. 暂无评论