【问题标题】:Select the text inside an input using Typescript in Angular 2在 Angular 2 中使用 Typescript 选择输入中的文本
【发布时间】:2016-07-07 11:04:14
【问题描述】:

我正在尝试完全按照this post 中的描述进行操作,但在 Angular2 中。

在用户单击触发器后,基本上在输入中使用 javascript 函数 .setSelectionRange(start, end);。我找不到任何方法来使用 Typescript 复制这种行为。

提前致谢。

【问题讨论】:

    标签: angularjs typescript angular


    【解决方案1】:

    我找不到使用 Typescript 复制此行为的任何方法。

    TypeScript 只是 JavaScript。我怀疑你的意思是说Angular2(那个帖子是Angular1)。

    角2

    您需要掌握 dom 元素(这似乎是您正在努力解决的问题)。在您的控制器中,您需要注入ElementRef。例如。 @Inject(ElementRef) elementRef: 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() {
    
      }
    }
    

    【讨论】:

    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2017-03-15
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多