javascript - Renderer2, ViewChild and ElementRef. Why do we needuse such things in Angular? - Stack Overflow

I'm quite new to Angular and I've discovered that there's something like Renderer2, View

I'm quite new to Angular and I've discovered that there's something like Renderer2, ViewChild and ElementRef for the sake of handling DOM rather than accessing it directly. And the articles I've e across say that manipulating DOM directly is a bad idea so we should use those Angular offering wrapper, service whatever it is. So it gets me wondering, in what point is handling DOM directly a bad idea? Any insight would be appreciate!

I'm quite new to Angular and I've discovered that there's something like Renderer2, ViewChild and ElementRef for the sake of handling DOM rather than accessing it directly. And the articles I've e across say that manipulating DOM directly is a bad idea so we should use those Angular offering wrapper, service whatever it is. So it gets me wondering, in what point is handling DOM directly a bad idea? Any insight would be appreciate!

Share Improve this question edited Apr 16, 2018 at 2:29 DongBin Kim asked Jan 20, 2018 at 4:47 DongBin KimDongBin Kim 1,9897 gold badges28 silver badges45 bronze badges 3
  • 1 I would say the primary reason is that Angular re-renders the view with it's change-detection cycle.So, changes (ideally) should happen in a way that's detectable by Angular. That said - there are some examples of direct manipulation of DOM in Angular that work fine, for example styling via a Directive (see Attribute Directives and search for nativeElement). You just have to know that what you change on DOM does not affect the change detection mechanism. – Richard Matsen Commented Jan 20, 2018 at 6:21
  • @RichardMatsen as far as I know, el(ElementRef type).nativeElement indicates/points the related DOM itself. So in my knowledge, manipulating DOM using nativeElement seems not that different from accessing DOM directly. What am I missing here? – DongBin Kim Commented Jan 23, 2018 at 4:07
  • You are missing nothing. nativeElement gives you access to the DOM from ElementRef or ViewChild. – Richard Matsen Commented Jan 23, 2018 at 4:11
Add a ment  | 

2 Answers 2

Reset to default 6

When we access the native element directly we are giving up on Angular’s DOM abstraction and miss out on the opportunity to be able to execute also in none-DOM environments such as native mobile, native desktop, web worker or server-side rendering.

Remember that Angular is a platform, and the browser is just one option for where we can render our app.

So what you do is to give this responsibility to these classes.

Handling the dom directly in Angular is a "bad idea" when you need server-rendering or using a webworker where it does not work.It also makes creating unit testing easier as you are not manipulating the dom.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745622052a4636578.html

相关推荐

  • 更正

    之前文章 地表最强基准-ADR1001 中:这个地方有错误业界流量很高的号,百花潭也转载了,不改正的话变成了错误永流传。有读者留言说,恒温控制晶振(OCXO)也有类似的设计:对于某些应用,TCXO(温度补偿)的频率-温度稳定性指标仍无法满足

    1小时前
    10
  • JUC并发—6.AQS源码分析二

    大纲1.ReentractReadWriteLock的基本原理2.基于AQS实现的ReentractReadWriteLock3.ReentractReadWriteLock如何竞争写锁4.ReentractReadWriteLock如何竞

    1小时前
    10
  • 解码陪玩系统连麦技术:从音频传输到实时交互

    陪玩公众号 陪玩小程序陪玩app陪玩平台源码 陪玩平台搭建 游戏陪玩源码 线上游戏陪玩源码 线上游戏平台搭建线上游戏代练系统 下线付费搭子源码 线下家政平台 线下家政源码线下预约服务源码 陪玩陪聊系统源码 陪玩平台搭建 游戏陪玩系统,线

    1小时前
    00
  • RAG 作者:RAG 已死,RAG 万岁!

    Datawhale分享 作者:Douwe Kiela,编译:思考机器本文作者 Douwe Kiela,RAG 论文(Retrieval-Augmented Generation for Knowledge-Intensive NLP Ta

    1小时前
    00
  • CAN总线仿真工程开发流程及策略

    在汽车电子控制系统的开发过程中,ECU的仿真测试起着至关重要的作用。总线仿真工程作为ECU开发中的核心环节,它贯穿了整个开发过程,从需求分析到软件开发、软件测试,再到硬件验证和失效分析等多个阶段。一个合理的总线仿真工程开发流程和科学的开发策

    1小时前
    00
  • 手把手教你使用 mcp

    手把手教你使用 mcp-server —— vscode + 多款 MCP Server 实现高德地图旅游攻略生成并分享给其他人Author:GoritDate:2025年4月24日目标:看完这篇文章你就能学会如何使用调用高德地图 MC

    1小时前
    00
  • minio使用简介

    在云原生和微服务时代,对象存储已成为存储非结构化数据(如图片、日志、备份等)的首选方案。MinIO 是一款高性能、兼容 S3 API 的开源对象存储服务,而它的官方 Go SDK —— minio-go,则可以让你在 Go 语言项目中轻松集

    1小时前
    00
  • Ascend 910b 运行vllm报错 libatb.so: cannot open shared object file

    ascend上运行vllm报错, 说找不到 so库OSError: libatb.so: cannot open shared object file: No such file or directory代码语言:shell复制Loadin

    1小时前
    00
  • 业内首次! 全面复现DeepSeek

    机器之心发布机器之心编辑部OpenAI 的 o1 系列和 DeepSeek-R1 的成功充分证明,大规模强化学习已成为一种极为有效的方法,能够激发大型语言模型(LLM) 的复杂推理行为并显著提升其能力。然而,这些推理模型的核心训练方法在其技

    58分钟前
    00
  • 仅用3周时间,就打造出Manus开源平替!贡献源代码,免费用

    机器之心报道机器之心编辑部AI 不再仅仅是一个工具,而是开始成为一个真正的队友。可以对标 Manus 的智能体 Suna 来了!(有没有发现它是 manus 倒过来写)这款智能体由 Kortix AI 团队打造,开源并且完全免费。image

    57分钟前
    00
  • 【一步步开发AI运动APP】八、自定义姿态动作识别检测——之姿态相似度比较

    之前我们为您分享了【一步步开发AI运动小程序】开发系列博文,通过该系列博文,很多开发者开发出了很多精美的AI健身、线上运动赛事、AI学生体测、美体、康复锻炼等应用场景的AI运动小程序;为了帮助开发者继续深耕AI运动领域市场,今天开始我们将为

    35分钟前
    00
  • dotnet 10 新的 JsonIgnoreCondition

    dotnet 10 新的 JsonIgnoreConditionIntro之前提了一个 api 建议为 JsonIgnore 添加两个扩展,WhenReading 和 WhenWriting,主要的一个用例是 WhenReading 我们的

    31分钟前
    00
  • 科研人狂喜!不用再到处找文献,这个神器让你轻松获取全网外文文献

    写论文时,你是否也有过这样的困扰?SCI - Hub?Library Genesis?几个数据库反复横跳,还是找不到想要的文献。想查最新的外文研究成果,却不知道该上哪个数据库,在各个平台间来回切换,浪费了大量时间和精力,结果还可能一无所获。

    26分钟前
    00
  • 15分钟快速了解 Odoo

    一、引言在企业数字化转型进程中,开源 ERP 系统 Odoo 因模块化设计灵活、功能扩展性强而备受青睐。但新用户在安装和体验 Odoo 时会遭遇难题,像基于 Docker 技术的传统部署,步骤繁琐、镜像拉取不易、配置复杂且管理不便,令许多人

    23分钟前
    00
  • Sentieon软件发布v202503版本

    Sentieon最新版本V202503Sentieon团队持续优化升级产品,现已发布v202503版本。本文将详细介绍此次更新中的重要功能改进和问题修复,以帮助您更好地了解和使用最新版本。图1Sentieon V202503版手册目录

    14分钟前
    00
  • Java与C语言核心差异:从指针到内存管理的全面剖析

    【前言】 在计算机编程领域,Java和C语言都是极具影响力的编程语言。Java以其跨平台性、安全性和面向对象的特性广受欢迎;而C语言凭借对底层的强大操控能力,在系统开发、嵌入式领域占据重要地位。这两种语言在指针和内存管理方面存在显著差异,

    11分钟前
    00
  • linux命令详细说明以及案例

    参考资料linux查看进程linux解压ziplinux详细说明以及案例centos7镜像下载详细说明以及案例centos 8详细说明以及案例centos 安装docker详细说明以及案例HTTP,TCP,UDP常见端口对照表centos

    9分钟前
    00
  • vscode如何多行同时编辑,vscode快速选中多行快捷键

    作者:watermelo37CSDN万粉博主、华为云云享专家、阿里云专家博主、腾讯云、支付宝合作作者,全平台博客昵称watermelo37。一个假装是giser的coder,做不只专注于业务逻辑的前端工程师,Java、Do

    6分钟前
    00
  • Mysql之存储过程

    1.存储过程概述 定义:MySQL 存储过程是一组预编译的 SQL 语句,它们被存储在数据库中,并可以通过调用来执行。用途:存储过程可以用于封装复杂的 SQL 操作、提高代码的重用性、增强数据操作的安全性和性能。特点:可以接受参数,也可以返

    4分钟前
    00
  • ComfyUI教程|基础篇:安装方法(Windows系统)

    前言 前言 ComfyUI作为一款功能强大的AI生图工具,它通过节点方式,使用户可以直观地看到各个模块的功能,并根据需求进行调整和连接。这种方法使工作流程更加清晰&

    4分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信