新手有点疑惑,索性查查资料总结一下和ng相关的几个和DOM相关的几个概念 ElementRef 由于ng是跨平台的为了减少视图层和渲染层的耦合也为了让ng更适应多平台,ng帮我们封装了ElementRef ,我们可以通过ElementRef拿到native元素(在浏览器中也就是我们常说的DOM元素) 下面我们看一段代码 import { Component, ElementRef, AfterViewInit
下面我们就来分析一下 ElementRef 类: ElementRef 的作用 在应用层直接操作 DOM,就会造成应用层与渲染层之间强耦合,导致我们的应用无法运行在不同环境,如 web worker 中 ElementRef 的定义 // angular-master/packages/core/src/linker/element_ref.ts export class ElementRef<T = : ElementRef) { let divEle = this.elementRef.nativeElement.querySelector('div'); console.dir( 沉思中… ,不是有 setTimeout 么,我们在稍微改造一下: constructor(private elementRef: ElementRef) { setTimeout(() => { = elementRef 是可选的,编译时会自动赋值 // function AppComponent(elementRef) { this.elementRef = elementRef; }
然后,在 javascript 文件 tooltip.component.ts 内容如下: import { Component, ElementRef, // 元素指向 HostBinding : string; constructor( private elementRef: ElementRef ) { } ngOnInit(): void { this.hostStyleTop ComponentFactoryResolver, // 创建组件对象 ComponentRef, // 组件实例的关联和指引,指向 ComponentFactory 创建的元素 Directive, ElementRef (); } constructor( protected elementRef: ElementRef, protected appRef: ApplicationRef, this.componentRef.instance.data = { // 绑定 data 数据 content: this.appTooltip, element: this.elementRef.nativeElement
方式一: ViewChild
@ViewChild('box') box: ElementRef; constructor(){ // 不能放在构造函数里面这个时候构造函数中还没有视图没法获取到 box元素 } ngOnInit() { console.log(this.box.nativeElement); } 方式二: ElementRef constructor(private el:ElementRef){ // 同理 } ngOnInit(){ console.log(this.el.nativeElement);ElementRef 在日常工作中,Web 工程师经常需要跟 DOM 打交道。 因此引入 ElementRef 类主要目的是为了实现跨平台。 : ElementRef) { console.log(this.elementRef); } } 以上代码运行后,控制台的输出结果: ElementRef {nativeElement: : ElementRef) { console.log(this.elementRef); } } 以上代码运行后,控制台的输出结果: ElementRef {nativeElement: , context)); } get elementRef(): ElementRef { return new ElementRef(asElementData(this.
此外还定义了以下引用类型:ElementRef、TemplateRef、ViewRef 、ComponentRef 和 ViewContainerRef 等。 为了演示,先定义一个组件DemoComponent: import { AfterViewInit, Component, ElementRef, Renderer2, ViewChild } from > ` }) export class DemoComponent implements AfterViewInit { @ViewChild('demoDiv') demoDiv: ElementRef @ViewChild @ViewChild('demoDiv') demoDiv: ElementRef; // @ViewChild通过模板变量名获取 ngAfterViewInit() { 对象,ElementRef 定义如下: class ElementRef<T> { constructor(nativeElement: T) nativeElement: T } 因此我们可以在
创建一个属性型指令 -- 初级应用 自己创建属性型指令的必要条件: import { Directive, ElementRef, Input, Renderer } from '@angular/core '; @Directive({ selector: '[myHighlight]' }) export class HighlightDirective { constructor(el: ElementRef ElementRef注入到指令构造函数中。 这样代码可以访问 DOM 元素。 Input将数据从绑定表达式传达到指令中。 Renderer让代码可以改变 DOM 元素的样式。 **ElementRef是一个服务,它赋予我们通过它的nativeElement属性直接访问 DOM 元素的能力。 ** Renderer服务允许通过代码设置元素的样式。
现在我们先来定义 CreditCardDirective: import { Directive, ElementRef } from '@angular/core'; @Directive({ selector: '[credit-card]' }) export class CreditCardDirective { constructor(private element: ElementRef red'; } 此时,CreditCardDirective 指令的功能已基本完成,下面是完整的实现: import { Directive, HostListener, HostBinding, ElementRef 下面我们来定义 TooltipDirective 指令: import { Input, Directive, ElementRef, OnInit } from '@angular/core'; @ ) {} } 接着我们按照上述的方案,更新一下 TooltipDirective 指令: import { Input, Directive, ElementRef, OnInit } from
这需要三个步骤: 定义访问元素const elementRef = useRef()的引用; 将引用赋给元素的ref属性:<div ref={elementRef}>
假如对指令有所了解了,那首先创建一个指令: ionic g directive autosize 打开文件编辑: import { Directive, ElementRef, HostListener onInput(textArea:HTMLTextAreaElement):void { this.adjust(); } constructor(public element:ElementRef
ionic-native/keyboard --save 然后创建指令: ionic g directive keyboardAttach 然后打开文件,修改为: import { Directive, ElementRef onShowSubscription: Subscription; private onHideSubscription: Subscription; constructor( private elementRef : ElementRef, private keyboard: Keyboard, private platform: Platform ) {} ngOnInit() { onHide() { this.setElementPosition(0); }; private setElementPosition(pixels: number) { this.elementRef.nativeElement.style.paddingBottom
在构造函数constructor加上一句,赋值默认颜色: import { Directive, Input, ElementRef } from '@angular/core'; @Directive this.el.nativeElement.style.backgroundColor = 'red'; } } 基于安全性考虑,angualr2的文档是建议用Renderer来代替ElementRef 那我们这样修改: import { Directive, Input, ElementRef } from '@angular/core'; @Directive({ selector: '[bg-color 为实现该功能,我们需要在事件处理函数上添加@HostListener装饰器,代码改动如下: import { Directive, Input, ElementRef, HostListener } (color:string){ this.setStyle(color); this.bgColor = color; }; constructor(private el: ElementRef
import { Component, ElementRef } from '@angular/core'; @Component({ selector: 'my-app', template p>Hello {{name}}
`, }) export class AppComponent { name: string = ''; constructor(public elementRef : ElementRef) { // 使用构造注入方式注入依赖对象 this.name = 'Semlinker'; // 执行数据初始化操作 } } ngOnInit 应用场景 在项目开发中我们要尽量保持构造函数简单明了Original" /> <canvas #canvasRef id='myCanvas'> </canvas> @ViewChild('imgRef', { static: false }) imgRef: ElementRef ; @ViewChild('canvasRef', { static: false }) canvasRef: ElementRef; const srcImg: HTMLImageElement | myImg' [src]="imageSrc" alt="Original" />
Shadow DOM的用法 向普通元素附着Shadow DOM let shadow = elementRef.attachShadow({mode:'open'}); 配置参数可选open或closed open模式下,elementRef.shadowRoot返回shadowRoot,而closed模式下返回null,因此如果使用closed模式,需要在建立ShadowDOM时保存引用(如上)。
</ion-thumbnail> </ion-col> </ion-row> 旧的ts这部分: width: string; @ViewChild('addImage') img: ElementRef this.width = this.img.nativeElement.width + 'px'; } 变为新的: @ViewChildren('image') childCmps: QueryList<ElementRef
const embeddedView = this.tpl.createEmbeddedView( null ); //模板的name显示不出, 传入this也不行 const p = this.tpl.elementRef.nativeElement.parentNode as HTMLElement; p.insertBefore(embeddedView.rootNodes[0], this.tpl.elementRef.nativeElement); // 方法
此文件的内容如下: import { Directive, ElementRef, HostListener } from '@angular/core'; @Directive({ selector : '[appDirectiveConsole]' }) export class DirectiveConsoleDirective { constructor( public elementRef : ElementRef ) { } @HostListener('keyup', ['$event.target']) keyupFunction(event: any) { if (event.value) { console.log(this.elementRef) this.elementRef.nativeElement.value = event.value.trim
Viewchild 和 ElementRef 在 ViewChild 小节,我们使用 @ViewChild(AuthMessageComponent) 装饰器来获取 AuthMessageComponent email> </label> 接下来更新 AuthFormComponent 组件类,使用 ViewChild 装饰器来获取邮箱输入框的元素引用: @ViewChild('email') email: ElementRef => { message.days = 30; }); this.cd.detectChanges(); } } 以上代码成功运行后,控制台会输出以下内容: ElementRef 此外还定义了以下引用类型:ElementRef、TemplateRef、ViewRef 、ComponentRef 和 ViewContainerRef 等。
判断传入的内容是否为url 创建一个悬浮tooltip 把对应的内容填充进去且可以访问跳转 ---- 实现的指令代码 import { Directive, // 创建一个指令必须依赖这个装饰器 ElementRef @Input() private appHoverText: any; public div: HTMLDivElement; constructor( private el: ElementRef