[Angular] Using the platform agnostic Renderer & ElementRef
2024-10-01 12:42:41
ElementRef:
ElementRef is a way to access native html element, notice that it only works for Broswer.
Render:
Render helps to take care of different platforms (mobile or browser). It is recommended to use Render to change DOM instead of using ElementRef directly.
<label>
Email address
<input type="email" name="email" ngModel #email>
</label>
export class AuthFormComponent implements AfterViewInit { @ViewChild('email') email: ElementRef; constructor(
private renderer: Renderer
) {} ngAfterViewInit() {
this.renderer.setElementAttribute(this.email.nativeElement, 'placeholder', 'Enter your email address');
this.renderer.setElementClass(this.email.nativeElement, 'email', true);
this.renderer.invokeElementMethod(this.email.nativeElement, 'focus');
// this.email.nativeElement.setAttribute('placeholder', 'Enter your email address');
// this.email.nativeElement.classList.add('email');
// this.email.nativeElement.focus();
} ... }
Another example:
this.loadingService.duration: '2s'
//from
this.el.nativeElement.style.setProperty(
"--duration",
this.loadingService.duration
); // to
this.render.setAttribute(
this.el.nativeElement,
"style",
`--duration:${this.loadingService.duration}`
);
最新文章
- vuex(1.0版本写法)
- RabbitMQ 集群安装过程详解
- Spring操作指南-针对JDBC配置声明式事务管理(基于XML)
- SQL Server备份脚本
- MVC5+EF6 Code First 从零开始——第一章
- Android 自定义波浪动画 --";让进度浪起来~";
- GUI用户界面编程
- 利用excel数据透视表实现快速统计相关数据
- python setup.py install 失败
- IOS 应用程序启动加载过程(从点击图标到界面显示)
- (转)Div+CSS布局入门
- JavaScript中的构造函数
- (精选)Xcode极速代码,征服Xcode,xcode插件
- Ocelot中文文档-GraphQL
- matplotlib.mlab库的重要函数
- gym 101755
- java中获取字母和数字的组合
- PMP 考试 形式
- synchronized、锁、多线程同步的原理是咋样
- git删除本地保存的账号和密码
热门文章
- Activity启动模式的深入分析
- Windows Server 2016 上配置 APACHE+SSL+PHP+perl
- Redis原理(二)
- Flask项目之手机端租房网站的实战开发(四)
- uva 1463 - Largest Empty Circle on a Segment(二分+三分+几何)
- Android EditText回车不换行
- (转)30 IMP-00019: row rejected due to ORACLE error 12899
- GDB中创建要素数据集
- 仿招商银行载入loading效果
- 高效的敏感词过滤方法(PHP)