[Angular 2] Style Angular 2 Components

Each Angular 2 Component can have its own styles which will remained contained inside the component. These isolated styles allow you to add whichever styles you want without worrying about them leaking out and affecting the rest of your application.

import {Component, Input} from "@angular/core";
@Component({
    selector: ‘widget-one‘,
    styles:[`
:host{
    display: block;
    border: 3px dashed black;
    font-family: "Times New Roman";
}
`],
    template: `
<h2>One‘s message:</h2>
<h3>{{message}}</h3>
`
})
export class WidgetOne{
    @Input() message;
}

Notice three things:

1. If you want to apply style to ‘widget-one‘ tag, which means the host element for the component, you need to use ‘:host‘.

2. The style apply only has affect to this component, won‘t have any effect to other component.

3. The host component doesn‘t have any ‘display‘ attr setup, so you need to set it as ‘flexbox‘ or ‘block‘, or any other display prop....

时间: 2024-10-05 04:58:28

[Angular 2] Style Angular 2 Components的相关文章

[Angular 2] Passing data to components with &#39;properties&#39;

Besides @Input(), we can also use properties on the @Component, to pass the data. import {Component, View, NgFor, Input} from 'angular2/angular2'; @Component({ selector: 'reddit-article' }) @View({ directives: [], template: ` <article> <div class

嘿!@野兽,你的ng api 掉了 - - angular.uppercase和angular.lowercase

@野兽的 ng api 学习 -- angular.uppercase和angular.lowercase angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = "ABCabc"; var upperCase = angular.uppercase(str);//ABCABC angular.lowercase 将指定的字符串转换成小写 格式:ang

嘿!@野兽,你的ng api 掉了 - - angular.identity和angular.noop

@野兽的 ng api 学习 -- angular.identity和angular.noop angular.identity 函数返回本身的第一个参数.这个函数一般用于函数风格. 格式:angular.identity() 使用代码: <script> angular.module("Demo", []) .controller("testCtrl", ["$scope", function ($scope) { var getR

angular.js 的angular.copy 、 angular.extend 、 angular.merge

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script src="js/angular.min.js" type="text/javascript" charset="utf-8"></s

Angular - - angular.injector、angular.module

angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules:  Array 注入的模块(一个或多个). 使用代码: (function () { angular.module("firstModule", []) .service("firstService", function () { this._log = functi

angular.equals()、angular.extend()、angular.foreach()、angular.fromJson()、angular.identity()等

angular.equals(o1, o2) 解释:参数o1和o2的比较(参数可以为变量.数组.对象) demo:angular.equals({name:'xxx'},{name:'yyy'}); //$ false angular.extend(dst, src) dst:被扩展的目标 src:扩展的对象 解释:对象的扩展,存在的类型进行值得覆盖,不存在的增加该类型. demo: var dst = {name: 'xxx', country: 'China'}; var src = {na

Angular JS - 6 - Angular JS 常用指令

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <!-- 8 1. Angular指令 9 * Angular为HTML页面扩展的: 自定义标签属性或标签 10 * 与Angular的作用域对象(scope)交互,扩展页面的动

[Angular 2] Generate Angular 2 Components Programmatically with entryComponents &amp; ViewContainerRef

You can generate Angular 2 components using a combination of ViewContainer and ComponentFactory, but you must always remember to add the components you want to generate to your list of entryComponents otherwise the compiler will optimize the componen

[Angular2 Router] Use Params from Angular 2 Routes Inside of Components

Angular 2’s ActivatedRoute allows you to get the details of the current route into your components. Params on the ActivatedRoute are provided as streams, so you can easily map the param you want off of the stream and display it in your template. For