javascript 在 textarea 插入符号处插入文本的 Angularjs 指令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21401042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angularjs directive to Insert text at textarea caret
提问by HP.
I have controller MyCtrland directive myText. When a model in MyCtrlchanges, I want to update the textareaof myTextdirective by inserting the text at current position / caret.
我有控制器MyCtrl和指令myText。当模型MyCtrl发生变化时,我想通过在当前位置/插入符号插入文本来更新textareaofmyText指令。
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="[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:
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 elementis not the DOM object. Here is the error:
它现在不起作用,因为element它不是 DOM 对象。这是错误:
TypeError: Object [object Object] has no method 'focus'
QUESTION:So my question is how to fix this? Or how to get the realDOM object from angular element object?
问题:所以我的问题是如何解决这个问题?或者如何从angular元素对象中获取真正的DOM对象?
回答by Florian
Short anwser:
简短的回答:
element[0]
would give you the actual DOM Element.
会给你实际的 DOM 元素。
Long answer:
长答案:
angular.elementalways provides a jqLiteselector, which is similiar to a result set given by a jQuery selection. It's a set of HTML elements.
angular.element总是提供一个jqLite选择器,它类似于 jQuery 选择给出的结果集。它是一组 HTML 元素。
The linkfunction of a method gets called with the following params: scope, element, attrsand ctrl.
该link方法的函数被调用具有以下PARAMS: ,,和。scopeelementattrsctrl
elementis given as a set of the one DOM Element the directive is attached to. So the first item is the actual HTML Element.
element作为指令附加到的一个 DOM 元素的集合给出。所以第一项是实际的 HTML 元素。

