This article mainly introduces how to avoid Dom misunderstandings in advanced Angular2. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and 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. Directly using javascript and jquery language is unstable because their compilation is not stable. Errors will be discovered in advance, so angular2 will choose the typescript language, a superset of javascript (errors can be found during compilation of this language).
2. 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 use DOM manipulation mechanism?
4. Correct way to operate DOM (ElementRef and Renderer2):
product.component.html<p>商品信息</p> <ul> <li *ngFor="let product of dataSource| async as list"> {{product.title}} </li> </ul> <p #dia> </p>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
Learning 1 In fact, for this language, we should first follow its specifications, accept its differences from previous languages, and then deeply understand how it is different from the previous language and why we do this. Otherwise, we will not be able to understand the beauty of this language. ,Hope it helps you!
##
The above is the detailed content of How to avoid Dom misunderstandings in advanced 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

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
