在Angular2中使用nativeElement访问DOM有哪些好的替代方法?(What are some good alternatives to accessing the DOM using

系统教程 行业动态 更新时间:2024-06-14 16:57:18
在Angular2中使用nativeElement访问DOM有哪些好的替代方法?(What are some good alternatives to accessing the DOM using nativeElement in Angular2?)

以下面的代码为例,它们是使用nativeElement访问DOM的好方法

import {Component, Directive, Input, ElementRef} from 'angular2/core';

@Directive({
    selector: '[myDR]',
    host:{
        '(mouseenter)' : ' mouseEnter()'
    }
})

export class MyDi {

    @Input () myColor: string;

    constructor(private el:ElementRef) {

    }

    mouseEnter(){

        this.el.nativeElement.style.backgroundColor = this.myColor;

        console.log(this.myColor);
     }
}
 

这是一款让您更轻松地测试的Plunker

Taking as an example the following code, which are good alternatives to accessing the DOM using nativeElement

import {Component, Directive, Input, ElementRef} from 'angular2/core';

@Directive({
    selector: '[myDR]',
    host:{
        '(mouseenter)' : ' mouseEnter()'
    }
})

export class MyDi {

    @Input () myColor: string;

    constructor(private el:ElementRef) {

    }

    mouseEnter(){

        this.el.nativeElement.style.backgroundColor = this.myColor;

        console.log(this.myColor);
     }
}
 

This is a Plunker for you test more easy.

最满意答案

由于不鼓励通过nativeElement直接访问DOM, nativeElement您有三个选项

使用host属性(这将立即设置颜色)
@Directive({
    host:{
        '(mouseenter)' : ' mouseEnter()',
        '[style.background-color]' : 'myColor'
    }
})

mouseEnter(){
    this.myColor = 'blue';
}
 
 
  使用@HostBinding (这种情况会立即设置颜色)  
 
@HostBinding('style.background-color') get color {
    return this.myColor;
}

mouseEnter(){
    this.myColor = 'blue';
}
 
 
  使用Renderer (使用此代替nativeElement.style = 'value' )  
 
constructor(public renderer: Renderer, public element: ElementRef) {}

mouseEnter(){
  this.renderer.setElementStyle(this.element.nativeElement, 'background-color', this.myColor);
}
 

请注意, host和@HostBinding是等效的。

Since accessing directly to DOM through nativeElement is discouraged you have three options

Using host property (this will set the color immediatly)
@Directive({
    host:{
        '(mouseenter)' : ' mouseEnter()',
        '[style.background-color]' : 'myColor'
    }
})

mouseEnter(){
    this.myColor = 'blue';
}
 
 
 Using @HostBinding (this case will set the color immediatly) 
 
@HostBinding('style.background-color') get color {
    return this.myColor;
}

mouseEnter(){
    this.myColor = 'blue';
}
 
 
 Using Renderer (use this instead of nativeElement.style = 'value') 
 
constructor(public renderer: Renderer, public element: ElementRef) {}

mouseEnter(){
  this.renderer.setElementStyle(this.element.nativeElement, 'background-color', this.myColor);
}
 

Note that host and @HostBinding are equivalent.

更多推荐

本文发布于:2023-04-12 20:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9dfe8268197e4f91cda7c237ce3a0e7c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   有哪些   DOM   nativeElement   alternatives

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!