This time I will bring you how to avoid Dom misunderstandings when using Angular2, and what are the precautions to avoid Dom misunderstandings when using Angular2. The following is a practical case, let's take a look.
Preface
The design goal of Angular2 is to make the browser and DOM independent. The DOM is complex, so decoupling components from it will make our applications easier to test and refactor. In order to support cross-platform, Angular also encapsulates the differences of different platforms through abstraction.Content
#1. Why can’t the DOM be manipulated directly?
Angular2 adopts AOT static compilation mode. In this form, our template type must be stable and safe. Directly2. Three incorrect ways to operate DOM:
@Component({ ... }) export class HeroComponent { constructor(private _elementRef: ElementRef) {} doBadThings() { $('id').click(); //jquery this._elementRef.nativeElement.xyz = ''; //原生的ElementRef document.getElementById('id'); //javascript } }
3. How does Angular2 DOM operate mechanism?
In order to support cross-platform, Angular encapsulates the differences of different platforms through abstraction layers. For example,4. Correct way to operate DOM (ElementRef and Renderer2):
product.component.html<p>商品信息</p>
- {{product.title}}
product.component.ts
import { Component, OnInit,Renderer2, ViewChild,ElementRef,AfterViewInit} from '@angular/core'; @Component({ selector: 'app-product', templateUrl: './product.component.html', styleUrls: ['./product.component.css'] }) export class ProductComponent implements OnInit,AfterViewInit { @ViewChild('dia') dia:ElementRef ;定义子试图 ngOnInit() { /**1. *创建一个文本 */ this.dia.nativeElement.innerHTML="这只是一个测试的文档"; /**2. *添加click事件 */ let ul=this.element.nativeElement.querySelector('ul'); this.render2.listen(ul,"click",()=>{ this.render2.setStyle(ul,"background","blue"); ngAfterViewInit(){ /**3. *修改背景颜色 */ let li=this.element.nativeElement.querySelector('ul'); this.render2.setStyle(li,"background","red"); } }
Summary
In fact, when learning a language, we should first follow its specifications, accept the differences between it and the previous language, and then deeply understand the differences between it and the previous language. What is the difference in the method and why do we do this? Otherwise we cannot understand the beauty of this language. I hope it will be helpful to you! I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:Usage of vue built-in instructions
The above is the detailed content of Avoid Dom misunderstandings when using Angular2. For more information, please follow other related articles on the PHP Chinese website!

JavaFX是Java平台的一个用户界面框架,类似于Swing,但却更加现代化和灵活。然而在使用时可能会遇到一些视图错误,本文将介绍如何处理和避免这些错误。一、JavaFX视图错误的类型在使用JavaFX时,可能会遇到以下几种视图错误:NullPointerException这是最常见的错误之一,通常在尝试访问未初始化或不存在的对象时发生。这可能

Java是一门非常流行的编程语言,许多项目都是由Java编写的。然而,当我们在开发过程中遇到“编解码错误”(EncodingandDecodingErrors)时,可能会感到困惑和疑惑。在本文中,我们将介绍Java编解码错误的原因、如何解决和避免这些错误。什么是编解码错误?在Java开发过程中,我们经常需要处理文本和文件。然而,不同的文本和文件可能使

随着Java的广泛应用,Java程序在连接数据库时经常会出现JDBC错误。JDBC(JavaDatabaseConnectivity)是Java中用于连接数据库的编程接口,因此,JDBC错误是在Java程序与数据库交互时遇到的一种错误。下面将介绍一些最常见的JDBC错误及如何解决和避免它们。ClassNotFoundException这是最常见的JDBC

Python变量命名规则的常见误区及解决方法在Python编程中,正确的变量命名是非常重要的。一个良好的命名习惯可以使代码更易读、易维护,并且可以避免一些潜在的错误。然而,新手常常会犯一些常见的变量命名误区。本文将介绍一些常见的误区,并给出解决方法和具体的代码示例。误区一:使用保留关键字作为变量名Python有一些保留关键字,这些关键字是Python语法中的

在PHP语言开发中,经常会遇到无限循环的情况,它会无限制地执行某些代码,导致程序崩溃甚至服务器崩溃。本文将介绍一些避免陷入无限循环的方法,帮助开发人员更好地解决这一问题。1.避免在循环中进行无限递归调用当在循环中调用一个函数或方法时,如果函数或方法中又包含了循环语句,就会形成无限递归调用,导致程序崩溃。为避免这种情况的发生,可以在递归调用函数或方法时,添加一

如何避免在PHP5.6升级至PHP7.4过程中出现的兼容性陷阱?随着技术的不断进步,PHP作为一种常用的编程语言,在不同的版本之间往往存在一些兼容性问题。当我们决定从较旧的版本升级到较新的版本时,很容易遇到一些意想不到的问题,特别是在PHP5.6升级至PHP7.4的过程中。为了帮助大家避免兼容性陷阱,本文将介绍一些常见的陷阱及其解决方法。语法错误PH

Golang是一种快速、高效的开发语言,以其强大的并发能力和内置的垃圾回收机制而受到广泛的欢迎。然而,即使在使用Golang进行开发时,仍然有可能遇到内存泄露的问题。本文将介绍一些常见的Golang开发注意事项,以帮助开发者避免内存泄露问题。避免循环引用循环引用是Golang中常见的内存泄露问题之一。当两个对象相互引用时,如果没有适时地释放这些对象的引用,就

PHP与MySQL索引的失效情况及如何避免和解决引言:在开发Web应用程序时,PHP和MySQL往往是常用的编程语言和数据库。而在处理大量数据时,索引是提高查询性能的重要因素之一。然而,索引的失效情况可能会导致查询变慢,从而影响应用程序的性能。本文旨在介绍PHP与MySQL索引的失效情况,并提供一些实用的解决办法和避免措施。一、什么是索引失效索引失效指的是在


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
