[Angular] How to styling ng-content

Let‘s say you are builing a reuseable component. The style structure like this:

div > input

If you want to style this input field, it can be quite easy, we can just use :host selector:

:host input {
  outline: none;
  border: none;
}

But one day you figure out that hard cord input into the component might not be a good idea. You want to use content projection ‘ng-content‘ to do that. Now the html stucture boecomes:

div > ng-content

Even you know the ng-content will be the input field. But he will find that your styling no longer works.

This is because Angular style encapsulation. It tries to preserve the style of projection element (because the element is not part of component, it has its own styling, we don‘t want to break that).

So the way to solve this problem is using CSS ‘/deep/‘ selector.

:host /deep/ input {
  outline: none;
  border: none;
}

‘/deep/‘ break angular style encapsulation. You can think it is a global css styling which can overwrite everything ..:P Sounds pretty prowerful, and a little bit damage, yes! it is.

For example, if you doning like this :

/deep/ input {
  outline: none;
  border: none;
}

You will find that, all the input fields in your app has been affected!! So to be safe, use with :host selector.

时间: 2024-08-14 01:17:03

[Angular] How to styling ng-content的相关文章

利用angular打造管理系统页面

1 创建一个新的angular应用 ng new adminSystem 2 利用WebStorm打开adminSystem应用 3 借助AdminLTE这个开源项目来辅助开发 AdminLTE项目:点击前往 将AdminLTE项目的精简版本源代码复制到adminSystem应用主模块的主组件的模板中 AdminLTE项目的精简版本效果图 <!DOCTYPE html> <!-- This is a starter template page. Use this page to star

Ngnice-国内ng学习网站

今天给angular新手介绍一个国内开源的ng学习网站http://www.ngnice.com/这是由一批ng爱好者在雪狼大叔的带领下共同开发完成,致力于帮助更多的ng新人,他们分别是: ckken,grahamle,NigelYao,asnowwolf,lightma,joeylin,FrankyYang,lrrluo, why520crazy,破狼,二当家, Ken, zxsoft, why520crazy, playing,天猪.jacobdong.以及一批后加入或审校未记名的社区爱好者

Angular学习笔记(工具篇)----Angular CLI

Angular CLI 的作用   首先安装npm 和node    详情:http://www.cnblogs.com/gorgeous/p/8074180.html 在安装    npm install -g @angular/cli 验证 创建Angular项目ng new my-app 进入Angular项目cd my-app 启动项目ng serve 优化项目创建方法(优化点:npm速度较慢) ng new my-app --skip-installcd my-appcnpm inst

Angular总结一:环境搭建

工欲善其事必先利其器,开发 Angular 项目首先要搭建环境.Angular 的环境搭建包括三个方面,开发环境 WebStorm,命令行工具 Angular CLI,以及 nodejs. 一.安装 nodejs 进入到 nodejs 的官网 https://nodejs.org/en/ 去下载安装包,下载慢的同学可以去我的百度网盘下载:https://pan.baidu.com/s/1mkilULq 密码:uaf7 根据电脑操作系统推荐了不同的版本,这里可以选择以上两个中的其中一个,下载完成后

Angular for MVC—项目搭建(1)

今天准备用angular+mvc创建一个项目,发现上次研究关于angular的东西全忘记了,突然想起上次我事后补了一篇博客的,看了一下也没太回忆起来细节.无奈,重新研究一边,这次是一边实践一边记录的,希望再过段时间我再来看的时候,希望能有帮助.我想记录博客目的也在于此.利于温故.你们要是觉得有用点个赞,没用勿喷,谢谢! 1.创建MVC项目 2.创建angular项目 2.1首先安装angular环境 下载安装node.js和npm,下载地址:https://nodejs.org/en/downl

Angular 中引入BootStrap

由于Bootstrap官方目前并没有发布Angular的相关类库进行支持,当前Angular只能引用使用Bootstrap相关的样式.无法使用Bootstrap自带的脚本逻辑.以下以Angular7和Bootsrap4.2为例进行demo验证. 环境搭建 首先执行以下两个命令创建angular项目和组件 ng new AngularDemo //创建项目 ng g c bootstrapdemo // 创建组件 然后执行 npm install bootstrap // 安装最新的bootstr

Angular常用命令

一. Angular常用命令 1. ng new 文件夹名 (新建项目,选择y使用路由) 2. ng serve --open (默认浏览器运行项目) 3. ng serve --port 6060  (运行多个项目在不同端口) 4. ng g (提示能创建的各种东西) 5. ng g component components/news (在app下生成components文件和news组件并自动导入组件至app.module.ts) 原文地址:https://www.cnblogs.com/d

再谈angularJS数据绑定机制及背后原理—angularJS常见问题总结

Angular 的数据绑定采用什么机制,详述原理? 脏检查机制.阐释脏检查机制,必须先了解如下问题. 单向绑定(ng-bind) 和 双向绑定(ng-model) 的区别? ng-bind 单向数据绑定($scope -> view),用于数据显示,简写形式是 {{}}. 两者的区别在于页面没有加载完毕 {{val}} 会直接显示到页面,直到 Angular 渲染该绑定数据(这种行为有可能将 {{val}} 让用户看到):而 ng-bind 则是在 Angular 渲染完毕后将数据显示. ng-

Angular--学习

18:28:34 Angular简介 AngularJS通过指令 扩展了HTML,并通过 表达式 绑定数据到HTML Angular扩展了HTML AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 AngularJS 应用程序. ng-model 指令把元素值(比如输入域的值)绑定到应用程序. ng-bind 指令把应用程序数据绑定到 HTML 视图. 实例 <!DOCTYPE html> <html lang="en&quo