简体   繁体   English

Angularjs指令在textarea插入符号中插入文本

[英]Angularjs directive to Insert text at textarea caret

I have controller MyCtrl and directive myText . 我有控制器MyCtrl和指令myText When a model in MyCtrl changes, I want to update the textarea of myText directive by inserting the text at current position / caret. MyCtrl的模型发生变化时,我想通过在当前位置/插入符号处插入文本来更新myText指令的textarea

I tried to reuse the code from this Inserting a text where cursor is using Javascript/jquery 我试图重用这个插入文本的代码, 其中游标使用Javascript / jquery

My Code: http://plnkr.co/edit/WfucIVbls2eekL8kUp7e 我的代码: http//plnkr.co/edit/WfucIVbls2eekL8kUp7e

Here is my HTML: 这是我的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="angular.js@1.2.x"></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: 使用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. 它现在不起作用,因为element不是DOM对象。 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? 或者如何从角元素对象中获取真正的 DOM对象?

Try: 尝试:

var domElement = element[0];

DEMO DEMO

Short anwser: 简短的anwser:

element[0]

would give you the actual DOM Element. 会给你实际的DOM元素。

Long answer: 答案很长:

angular.element always provides a jqLite selector, which is similiar to a result set given by a jQuery selection. angular.element总是提供一个jqLite选择器,它jqLite jQuery选择给出的结果集。 It's a set of HTML elements. 它是一组HTML元素。

The link function of a method gets called with the following params: scope , element , attrs and ctrl . 使用以下参数调用方法的link函数: scopeelementattrsctrl

element is given as a set of the one DOM Element the directive is attached to. element作为指令附加到的一个DOM元素的集合给出。 So the first item is the actual HTML Element. 所以第一项是实际的HTML元素。

I changed your code so that it should(?) work 我改变了你的代码,它应该(?)工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM