angularjs - 在 Angular 2 中使用 Typescript 选择输入内的文本

标签 angularjs typescript angular

我正在尝试完全按照 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/

相关文章:

javascript - 刷新时获取json数据

javascript - 错误 : [$injector:unpr] Unknown provider -> because ui-router requests the wrong thing

node.js - 升级到Angular 5后编译错误

oop - 使用 oop 继承的 angularjs

javascript - 如何在表体中使用 ng-repeat 来包含多行?

javascript - AngularJS 通过 AJAX 调用加载多个项目

javascript - JavaScript/TypeScript/Angular4 中的对象 URL 映射

typescript - 如何在 Nestjs 上使用具有多个 dto 的一条路由?

Angular react 形式

angular - 扩展基类与在 Typescript 中注入(inject)它