template分支,用12个例子全面示范Angular的模板语法

// 使用方法
git clone https://git.oschina.net/mumu-osc/learn-component.git
cd learn-component
git pull origin template
npm install
ng serve

1.基本插值语法

// test-interpolation.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'test-interpolation',
templateUrl: './test-interpolation.component.html',
styleUrls: ['./test-interpolation.component.css']
})
export class TestInterpolationComponent implements OnInit {
public title = '假的星际争霸2';
constructor() { } ngOnInit() {
} public getVal():any{
return 65535;
}
}
<!-- test-interpolation.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">基本插值语法</div>
<div class="panel-body">
<h3>
欢迎来到{{title}}!
</h3>
<h3>1+1={{1+1}}</h3>
<h3>可以调用方法{{getVal()}}</h3>
</div>
</div>

2.模板内的局部变量

// test-temp-ref-var.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'test-temp-ref-var',
templateUrl: './test-temp-ref-var.component.html',
styleUrls: ['./test-temp-ref-var.component.css']
})
export class TestTempRefVarComponent implements OnInit { constructor() { } ngOnInit() {
} public sayHello(name:string):void{
alert(name);
}
}
<!-- test-temp-ref-var.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">模板内的局部变量</div>
<div class="panel-body">
<input #heroInput>
<p>{{heroInput.value}}</p>
<button class="btn btn-success" (click)="sayHello(heroInput.value)">局部变量</button>
</div>
</div>

3.单向值绑定

// test-value-bind.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'test-value-bind',
templateUrl: './test-value-bind.component.html',
styleUrls: ['./test-value-bind.component.css']
})
export class TestValueBindComponent implements OnInit {
public imgSrc:string="./assets/imgs/1.jpg"; constructor() { } ngOnInit() {
} public changeSrc():void{
if(Math.ceil(Math.random()*1000000000)%2){
this.imgSrc="./assets/imgs/2.jpg";
}else{
this.imgSrc="./assets/imgs/1.jpg";
}
}
}
<!-- test-value-bind.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">单向值绑定</div>
<div class="panel-body">
<img [src]="imgSrc" />
<button class="btn btn-success" (click)="changeSrc()">修改图片src</button>
</div>
</div>

4.事件绑定

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'test-event-binding',
templateUrl: './test-event-binding.component.html',
styleUrls: ['./test-event-binding.component.css']
})
export class TestEventBindingComponent implements OnInit { constructor() { } ngOnInit() {
} public btnClick(event):void{
alert("测试事件绑定!");
}
}
<div class="panel panel-primary">
<div class="panel-heading">事件绑定</div>
<div class="panel-body">
<button class="btn btn-success" (click)="btnClick($event)">测试事件</button>
</div>
</div>

5.双向绑定

// test-twoway-binding.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'test-twoway-binding',
templateUrl: './test-twoway-binding.component.html',
styleUrls: ['./test-twoway-binding.component.css']
})
export class TestTwowayBindingComponent implements OnInit {
public fontSizePx:number=14; constructor() { } ngOnInit() {
} }
<!-- test-twoway-binding.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">双向绑定</div>
<div class="panel-body">
<font-resizer [(size)]="fontSizePx"></font-resizer>
<div [style.font-size.px]="fontSizePx">Resizable Text</div>
</div>
</div>
// font-resizer.component.ts
import { Component, OnInit, EventEmitter, Input, Output } from '@angular/core'; @Component({
selector: 'font-resizer',
templateUrl: './font-resizer.component.html',
styleUrls: ['./font-resizer.component.css']
})
export class FontResizerComponent implements OnInit {
@Input() size: number | string;
@Output() sizeChange = new EventEmitter<number>(); constructor() { } ngOnInit() {
} dec() { this.resize(-1); }
inc() { this.resize(+1); } resize(delta: number) {
this.size = Math.min(40, Math.max(8, +this.size + delta));
this.sizeChange.emit(this.size);
}
}
<!-- font-resizer.component.html -->
<div style="border: 2px solid #333">
<p>这是子组件</p>
<button (click)="dec()" title="smaller">-</button>
<button (click)="inc()" title="bigger">+</button>
<label [style.font-size.px]="size">FontSize: {{size}}px</label>
</div>

6.*ngIf的用法

// test-ng-if.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'test-ng-if',
templateUrl: './test-ng-if.component.html',
styleUrls: ['./test-ng-if.component.css']
})
export class TestNgIfComponent implements OnInit {
public isShow:boolean=true; constructor() { } ngOnInit() {
} public toggleShow():void{
this.isShow=!this.isShow;
}
}
<div class="panel panel-primary">
<div class="panel-heading">*ngIf的用法</div>
<div class="panel-body">
<p *ngIf="isShow" style="background-color:#ff3300">显示还是不显示?</p>
<button class="btn btn-success" (click)="toggleShow()">控制显示隐藏</button>
</div>
</div>

7.*ngFor用法

// test-ng-for.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'test-ng-for',
templateUrl: './test-ng-for.component.html',
styleUrls: ['./test-ng-for.component.css']
})
export class TestNgForComponent implements OnInit {
public races:Array<any>=[
{name:"人族"},
{name:"虫族"},
{name:"神族"}
]; constructor() { } ngOnInit() {
} }
<!-- test-ng-for.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">*ngFor用法</div>
<div class="panel-body">
<h3>请选择一个种族</h3>
<ul>
<li *ngFor="let race of races;let i=index;">
{{i+1}}-{{race.name}}
</li>
</ul>
</div>
</div>

8.NgClass用法

// test-ng-class.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'test-ng-class',
templateUrl: './test-ng-class.component.html',
styleUrls: ['./test-ng-class.component.scss']
})
export class TestNgClassComponent implements OnInit {
public currentClasses: {}; public canSave: boolean = true;
public isUnchanged: boolean = true;
public isSpecial: boolean = true; constructor() { } ngOnInit() { } setCurrentClasses() {
this.currentClasses = {
'saveable': this.canSave,
'modified': this.isUnchanged,
'special': this.isSpecial
};
}
}
<!-- test-ng-class.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">NgClass用法</div>
<div class="panel-body">
<div [ngClass]="currentClasses">同时批量设置多个样式</div>
<button class="btn btn-success" (click)="setCurrentClasses()">设置</button>
</div>
</div>

9.NgStyle用法

// test-ng-style.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'test-ng-style',
templateUrl: './test-ng-style.component.html',
styleUrls: ['./test-ng-style.component.css']
})
export class TestNgStyleComponent implements OnInit {
public currentStyles: {}
public canSave:boolean=false;
public isUnchanged:boolean=false;
public isSpecial:boolean=false; constructor() { } ngOnInit() {
} setCurrentStyles() {
this.currentStyles = {
'font-style': this.canSave ? 'italic' : 'normal',
'font-weight': !this.isUnchanged ? 'bold' : 'normal',
'font-size': this.isSpecial ? '24px' : '12px'
};
}
}
<!-- test-ng-style.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">NgStyle用法</div>
<div class="panel-body">
<div [ngStyle]="currentStyles">
用NgStyle批量修改内联样式!
</div>
<button class="btn btn-success" (click)="setCurrentStyles()">设置</button>
</div>
</div>

10.NgModel的用法

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'test-ng-model',
templateUrl: './test-ng-model.component.html',
styleUrls: ['./test-ng-model.component.css']
})
export class TestNgModelComponent implements OnInit {
public currentRace:any={name:"随机种族"}; constructor() { } ngOnInit() {
} }
<div class="panel panel-primary">
<div class="panel-heading">NgModel的用法</div>
<div class="panel-body">
<p class="text-danger">ngModel只能用在表单类的元素上面</p>
<input [(ngModel)]="currentRace.name">
<p>{{currentRace.name}}</p>
</div>
</div>

11.管道的用法

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'test-pipe',
templateUrl: './test-pipe.component.html',
styleUrls: ['./test-pipe.component.css']
})
export class TestPipeComponent implements OnInit {
public currentTime: Date = new Date(); constructor() {
window.setInterval(
()=>{this.currentTime=new Date()}
,1000);
} ngOnInit() {
}
}
<div class="panel panel-primary">
<div class="panel-heading">管道的用法</div>
<div class="panel-body">
{{currentTime | date:'yyyy-MM-dd HH:mm:ss'}}
</div>
</div>

12.安全取值

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'test-safe-nav',
templateUrl: './test-safe-nav.component.html',
styleUrls: ['./test-safe-nav.component.css']
})
export class TestSafeNavComponent implements OnInit {
public currentRace:any=null;//{name:'神族'}; constructor() { } ngOnInit() {
} }
<div class="panel panel-primary">
<div class="panel-heading">安全取值</div>
<div class="panel-body">
你选择的种族是:{{currentRace?.name}}
</div>
</div>

用12个例子全面示范Angular的模板语法的更多相关文章

  1. Angular 5.x 学习笔记(1) - 模板语法

    Angular 5.x Template Syntax Learn Note Angular 5.x 模板语法学习笔记 标签(空格分隔): Angular Note on github.com 上手 ...

  2. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(12)-实现用户异步登录和T4模板

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(12)-实现用户异步登录和T4模板 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建  ...

  3. angular 模板语法(官方文档摘录)

    https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...

  4. 12个免费的 Twitter Bootstrap 后台模板

    在互联网上提供很多免费的 Bootstrap 管理后台主题.所有你需要做的就是将它们下载并安装它们,这真的不是什么难事.问题是如何寻找到能够完美符合您的网站需求的主题.当然,你可以自己制作自定义的主题 ...

  5. Angular - Templates(模板)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 在Angular中,模板是一个包含了Angular特定元素和属性的HTML.Angula ...

  6. angular 缓存模板 ng-template $templateCache

    由于浏览器加载html模板是异步加载的,如果加载大量的模板会拖慢网站的速度,这里有一个技巧,就是先缓存模板. 使用angular缓存模板主要有三种方法: 方法一:通过script标签引入 <sc ...

  7. Angular for TypeScript 语法快速指南 (基于2.0.0版本)

    引导 import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; platformBrowserDynami ...

  8. python——sklearn完整例子整理示范(有监督,逻辑回归范例)(原创)

    sklearn使用方法,包括从制作数据集,拆分数据集,调用模型,保存加载模型,分析结果,可视化结果 1 import pandas as pd 2 import numpy as np 3 from ...

  9. 图数据库orientDB(1-2)例子

    http://gog.orientdb.com/index.html#/infotab 小朱25岁,出生在教师家庭并且有个姐姐小田,他现在奋斗在帝都.  那么SQL是这样滴!!! CREATE VER ...

随机推荐

  1. JBoss环境搭建及部署Web项目

    http://blog.csdn.net/pop303/article/details/7210290 赶在年前学习了一下JBOSS,之前觉得JBOSS相关资料会有很多,不过现在发现很少,在亚马逊出也 ...

  2. canvas学习笔记(上篇)-- canvas入门教程 -- canvas标签/方块/描边/路径/圆形/曲线

    [上篇] -- 建议学习时间4小时  课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...

  3. ASP.NET MVC使用IoC

    也许你会问ASP.NET MVC为什么会爱上IoC? 相爱的理由常常很简单,就像一首歌中所唱——“只为相遇那一个眼神”. 而ASP.NET MVC爱上IoC只为IoC能实现MVC控制器的依赖注入. 下 ...

  4. laravel数据库操作sql语句用Eloquent ORM来构造

    现在有查询语句: SELECT users.sNmame, users.iCreateTime, users_ext.iAge, users_ext.sSex FROM users LEFT JOIN ...

  5. Windows+Nginx+IIS做图片分布式存储详细步骤

    最近几天,一直在学习nginx在windows平台下的使用,为了寻找几种大量图片分布式存储而且有相对简单的存储方案 nginx是一种,还找到一种MongoDB GridFS 这两种方案我还是比较中意的 ...

  6. 使用PHP创建一个REST API(Create a REST API with PHP)

    译者前言: 首先这是一篇国外的英文文章,非常系统.详尽的介绍了如何使用PHP创建REST API,国内这方面的资料非常非常的有限,而且基本没有可操作性.这篇文章写的非常好,只要对PHP稍有了解的程序员 ...

  7. 手动安装minGW

    minGW是C语言编译包,将GCC编译器在Windows平台上编译软件提供支持. 手工安装minGW是一件很繁琐的事情,但是搞懂它很有用,因为C语言本身是一个很小的语法系统,全靠 各种库在支持,安装m ...

  8. CCNA2.0笔记_OSPF v3

    OSPF v3 是可以在ipv6上实现路由的一种路由协议 OSPF v2(for IPv4),OSPF v3(for IPv6)在一台路由器中互相独立运行 OSPF v3与v2有很多类似的功能: - ...

  9. Makefile 多个目标匹配的问题

    在windows下直接使用mingw32-make # ZTHREAD_A the static link library file of ZThread ZTHREAD_A = F:/ZJ/tool ...

  10. github 修改项目默认语言

    我们在提交到github上的项目有时候被识别成了其它的语言,非我们使用的语言,这个时候可以采取以下措施来强制将语言改成我们需要的语言 在项目中创建一个文件 .gitattributes 打开.gita ...