[Angular 2] Controlling how Styles are Shared with View Encapsulation
Style and View Encapsulation is best understood by seeing how each option (Emulated, Native, and None) compare to each other.
<html> <head> <title>Angular 2 QuickStart</title> <style> .started{ background-color: #0b97c4; } .completed{ background-color: #80d8ff; } </style> <script src="../node_modules/systemjs/dist/system.src.js"></script> <script src="../node_modules/angular2/bundles/angular2.dev.js"></script> <script> System.config({ packages: {'app': {defaultExtension: 'js'}} }); System.import('app/app'); </script> </head> <div class="started">Started</div> <div class="completed">Completed</div> <body> <app>Loading...</app> </body> </html>
import {Input, Component, View, NgClass, ViewEncapsulation} from "angular2/angular2"; import {TodoModel} from './todoService'; @Component({ selector: 'todo-item-render' }) @View({ encapsulation: ViewEncapsulation.Emulated, // default state: the global style will have an affect on compoment style directives: [NgClass], styles: [` .${TodoModel.STARTED}{ color: green; } .${TodoModel.COMPLETED}{ text-decoration: line-through; } `], template: ` <div> <span [ng-class]="todoinput.status">{{todoinput.title}}</span> <button (click)="todoinput.toggle()">Toggle</button> </div> ` }) export class TodoItemRender{ @Input() todoinput: TodoModel; }
if we switch to Native:
encapsulation: ViewEncapsulation.Native, // Use shadow down, which mean the global style will not affect the compoment sytle
last, we switch to None:
encapsulation: ViewEncapsulation.None, // the component style and global style are the same, affect each other
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2013-10-28 Dynamic Programming for TSP