[Angular] Use Angular’s @HostBinding and :host(...) to add styling to the component itself
One thing that we can do is to add styles directly to HTML elements that live within our component. However, in this lesson we see that style classes added to your template HTML within your component get applied to an inner <div> and not your component host. We will learn how to use :host(...) and @HostBinding to add styling directly to the component host itself.
import { Component, HostBinding } from '@angular/core';
@Component({
selector: 'host-styling',
template: `
<div>
I'm a div that wants to be styled
<button (click)="redFont = !redFont">toggle</button>
</div>
`,
styles: [
`
/* Apply this style if .yellow-style is added to the component tag itself */
:host {
background-color: yellow;
display:block;
}
:host(.red-font) {
color: red;
}
`
]
})
export class HostStylingComponent {
@HostBinding ('class.red-font') redFont = true;
}
We can use :host(<class-name>) with @HostBinding, it will add .red-font class to the host element based on condition.
[Angular] Use Angular’s @HostBinding and :host(...) to add styling to the component itself的更多相关文章
- [Angular 2] Angular 2 Smart Components vs Presentation Components
Both Smart Components and Presentation Components receive data from Services in entirely different w ...
- ASP.NET Core 2.1 Web API + Identity Server 4 + Angular 6 + Angular Material 实战小项目视频
视频简介 ASP.NET Core Web API + Angular 6的教学视频 我是后端开发人员, 前端的Angular部分讲的比较差一些, 可以直接看代码!!!! 这是一个小项目的实战视频, ...
- angular 2 angular quick start Could not find HammerJS
Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ...
- [Angular] Control the dependency lookup with @Host, @Self, @SkipSelf and @Optional
Very differently to AngularJS (v1.x), Angular now has a hierarchical dependency injector. That allow ...
- [Angular] Refactor Angular Component State Logic into Directives
Allow the base toggle to be a tag (<toggle>) or attribute (<div toggle>). The <toggle ...
- angular+selecte2(angular ng-repeat渲染)
一.页面代码 <select id="sponsorId" select2 ng-model="sponsorSelectedObj" ng-change ...
- Intergate flot with Angular js ——Angular 图形报表
下面这篇文章最终的结论就是 Flot 插件 结合 Angular 的Directive 来处理 图表的绘制 给出github上的一个demo源码.https://gist.github.com/fly ...
- Angular - - ngRoute Angular自带的路由
ngRoute $routeProvider 配置路由的时候使用. 方法: when(path,route); 在$route服务里添加一个新的路由. path:该路由的路径. route:路由映射信 ...
- @angular/cli (angular脚手架) 如何降级
1.卸载 npm uninstall -g @angular/cli 2.清除缓存 npm cache verify 3.查看是否卸载成功 ng v //如果显示ng 不是内部或外部的指令 则证明卸载 ...
随机推荐
- Centos6.5 安装lamp环境
转载自:http://www.jb51.net/article/37987.htm (转载请注明出处,谢谢) 准备篇: 1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/i ...
- CCF模拟题 最大的矩形
最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方 ...
- android ActionBar的使用
Action Bar主要功能包括: 1. 显示选项菜单 2. 提供标签页的切换方式的导航功能,能够切换多个fragment. 3. 提供下拉的导航条目. 4. 提供交互式活动视图取 ...
- java.lang.IllegalArgumentException: The observer is null.终于解决方式
java.lang.IllegalArgumentException: The observer is null.终于解决方式 在使用数据适配的时候的问题: java.lang.IllegalArgu ...
- hdu5024
思路要开阔些,或者说要转化一下思路,别太死 把每一个点当拐点,爆一边就能够.用记忆化搜索也行.都不会超时 #include<bits/stdc++.h> using namespace s ...
- 聊聊高并发(十八)理解AtomicXXX.lazySet方法
看过java.util.concurrent.atomic包里面各个AtomicXXX类实现的同学应该见过lazySet方法.比方AtomicBoolean类的lazySet方法 public fin ...
- Excel的版本
https://en.wikipedia.org/wiki/Microsoft_Excel 取自维基百科,需要特别注意的是,从v12开始,有很大的改变.后缀名从xls变为xlsx Versions 5 ...
- 6.Maven之(六)setting.xml配置文件详解
转自:https://blog.csdn.net/u012152619/article/details/51485152
- ajax模仿iframe
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- js函数的解析与执行过程
function f(a,b,c){ alert(a);//函数字符串 alert(b); var b = 5; function a(){ } } f(1,2); //预处理 lexicalEnvi ...