我正在尝试完全按照 this post 中的描述进行操作, 但在 Angular2 中。
基本上在用户单击触发器后在输入中使用 javascript 函数 .setSelectionRange(start, end);
。我找不到任何方法来使用 Typescript 复制此行为。
提前致谢。
最佳答案
I can't find any way to replicate this behaviour using Typescript.
TypeScript 就是 JavaScript。我怀疑你的意思是说 Angular2
(那个帖子是 Angular1)。
Angular 2
您需要掌握 dom 元素(这似乎是您正在努力解决的问题)。在您的 Controller 中,您需要注入(inject) ElementRef
。例如。 @Inject(ElementRef) 元素引用:元素引用,
一旦您拥有该元素,您就可以遍历它并执行您需要执行的任何 dom 访问/手动操作。
更多
文档:https://angular.io/docs/js/latest/api/core/ElementRef-class.html
例子
样本:https://stackoverflow.com/a/32709672/390330
import {Component, ElementRef} from 'angular2/core';
@Component({
selector:'display',
template:`
<input #myname (input) = "updateName(myname.value)"/>
<p> My name : {{myName}}</p>
`
})
class DisplayComponent implements OnInit {
constructor(public element: ElementRef) {
this.element.nativeElement // <- your direct element reference
}
ngOnInit() {
}
}
关于angularjs - 在 Angular 2 中使用 Typescript 选择输入内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36122558/