详解Angular中的模板语法

本篇文章给大家详细介绍一下angular中的模板语法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

详解Angular中的模板语法

相关教程推荐:《angular教程》

插值表达式

test-interpolation.component.ts

@Component({  selector: 'app-test-interpolation',  templateUrl: './test-interpolation.component.html',  styleUrls: ['./test-interpolation.component.css']})export class TestInterpolationComponent implements OnInit {  title = '插值表达式';  constructor() { }  ngOnInit() {  }  getValue(): string {    return '值';  }}

登录后复制test-interpolation.component.html

  
基插值语法
  
    

      欢迎来到 {{title}}!    

    

2+2 = {{2 + 2}}

    

调用方法{{getValue()}}

  

登录后复制

模板变量

test-template-variables.component.ts

@Component({  selector: 'app-test-template-variables',  templateUrl: './test-template-variables.component.html',  styleUrls: ['./test-template-variables.component.css']})export class TestTempRefVarComponent implements OnInit {  constructor() { }  ngOnInit() {  }  public saveValue(value: string): void {    console.log(value);  }}

登录后复制test-template-variables.component.html

  
模板变量
  
        

{{templateInput.value}}

      

登录后复制

值绑定、事件绑定、双向绑定

值绑定:[]

test-value-bind.component.ts

@Component({  selector: 'app-test-value-bind',  templateUrl: './test-value-bind.component.html',  styleUrls: ['./test-value-bind.component.css']})export class TestValueBindComponent implements OnInit {  public imgSrc = './assets/imgs/1.jpg';  constructor() { }  ngOnInit() {  }}

登录后复制test-value-bind.component.html

  
单向值绑定
  
    详解Angular中的模板语法  

登录后复制

事件绑定:()

test-event-bind-component.ts

@Component({  selector: 'app-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: any): void {    console.log(event + '测试事件绑定!');  }}

登录后复制test-event-bind.component.html

事件绑定

登录后复制

双向绑定:[()]

test-twoway-binding.component.ts

@Component({  selector: 'app-test-twoway-binding',  templateUrl: './test-twoway-binding.component.html',  styleUrls: ['./test-twoway-binding.component.css']})export class TestTwowayBindingComponent implements OnInit {  public fontSizePx = 14;  constructor() { }  ngOnInit() {  }}

登录后复制test-twoway-binding.component.html

  
双向绑定
  
        
Resizable Text
  

登录后复制font-resizer.component.ts

@Component({  selector: 'app-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();  constructor() { }  ngOnInit() {  }  decrement(): void {    this.resize(-1);  }  increment(): void {    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

  

这是子组件

      

登录后复制

内置结构型指令

*ngIf

test-ng-if.component.ts

@Component({  selector: 'app-test-ng-if',  templateUrl: './test-ng-if.component.html',  styleUrls: ['./test-ng-if.component.css']})export class TestNgIfComponent implements OnInit {  isShow = true;  constructor() { }  ngOnInit() {  }}

登录后复制test-ng-if.component.html

  
*ngIf的用法
  
    

显示内容

  

登录后复制

*ngFor

test-ng-for.component.ts

@Component({  selector: 'app-test-ng-for',  templateUrl: './test-ng-for.component.html',  styleUrls: ['./test-ng-for.component.css']})export class TestNgForComponent implements OnInit {  races = [    {name: 'star'},    {name: 'kevin'},    {name: 'kent'}  ];  constructor() { }  ngOnInit() {  }}

登录后复制test-ng-for.component.html

  
*ngFor用法
  
    

名字列表

    
          
  •        {{i}}-{{name.name}}      
  •     
  

登录后复制

ngSwitch

test-ng-switch.component.ts

@Component({  selector: 'app-test-ng-switch',  templateUrl: './test-ng-switch.component.html',  styleUrls: ['./test-ng-switch.component.css']})export class TestNgSwitchComponent implements OnInit {  status = 1;  constructor() { }  ngOnInit() {  }}

登录后复制test-ng-switch.component.html

  
ngSwitch用法
  
    
      

Good

      

Bad

      

Exception

    
  

登录后复制

内置属性型指令

HTML 属性与 DOM 属性关系

少量的 HTML 属性与 DOM 属性之间有着一对一的映射关系, 如 id;有些 HTML 属性没有对应的 DOM 属性, 如 colspan;有些 DOM 属性没有对应的 HTML 属性, 如 textContent;就算名字相同, HTML 属性和 DOM 属性也不是同一样东西;HTML 属性的值指定了初始值, DOM 属性的值表示当前值; HTML 属性的值不可改变, DOM 属性的值可以改变。模板绑定是通过 DOM 属性和事件来工作的, 而不是 HTML 属性。

注意: 插值表达式与属性绑定是同一个东西, 插值表达式属于 DOM 属性绑定。

NgClass

test-ng-class.component.ts

@Component({  selector: 'app-test-ng-class',  templateUrl: './test-ng-class.component.html',  styleUrls: ['./test-ng-class.component.scss']})export class TestNgClassComponent implements OnInit {  public currentClasses: {};  public canSave = true;  public isUnchanged = true;  public isSpecial = true;  constructor() { }  ngOnInit() {    this.currentClasses = {      'saveable': this.canSave,      'modified': this.isUnchanged,      'special': this.isSpecial    };  }}

登录后复制test-ng-class.component.html

  
NgClass用法
  
    
设置多个样式
    
  

登录后复制test-ng-class.component.less

.saveable {    font-size: 18px;}.modified {    font-weight: bold;}.special {    background-color: #ff3300;}

登录后复制

NgStyle

test-ng-style.component.ts

@Component({  selector: 'app-test-ng-style',  templateUrl: './test-ng-style.component.html',  styleUrls: ['./test-ng-style.component.css']})export class TestNgStyleComponent implements OnInit {  currentStyles: { };  canSave = false;  isUnchanged = false;  isSpecial = false;  constructor() { }  ngOnInit() {    this.currentStyles = {      'font-style': this.canSave ? 'italic' : 'normal',      'font-weight': !this.isUnchanged ? 'bold' : 'normal',      'font-size': this.isSpecial ? '36px' : '12px'    };  }}

登录后复制test-ng-style.component.html

  
NgStyle用法
  
    
      用NgStyle批量修改内联样式!    
    
  

登录后复制

NgModel

test-ng-model.component.ts

@Component({  selector: 'app-test-ng-model',  templateUrl: './test-ng-model.component.html',  styleUrls: ['./test-ng-model.component.css']})export class TestNgModelComponent implements OnInit {  name = 'kevin';  constructor() { }  ngOnInit() {  }}

登录后复制test-ng-model.component.html

    
NgModel的用法
    
        

ngModel只能用在表单类的元素上面

            

登录后复制

小工具

管道

Angular 内置的常用管道:

uppercase 与 lowercase

uppercase 将字母转换成大写 {{‘aaa’ | uppercase}} lowercase 将字母转换成小写 {{‘BBB’ | lowercase}}

Date

{{ birthday | date: ‘yyyy-MM-dd HH:mm:ss’}}

number

{{ pi | number: ‘2.2-2’}}
2.2-2: 表示是保留 2 位整数和 2 位小数。
2-2: 表示最少 2 位小数,最大 2 位小数。

示例

test-pipe.component.ts

@Component({  selector: 'app-test-pipe',  templateUrl: './test-pipe.component.html',  styleUrls: ['./test-pipe.component.css']})export class TestPipeComponent implements OnInit {  currentTime: Date = new Date();    str = 'aaa';  money = 34.567;  constructor() {  }  ngOnInit() {    window.setInterval(      () => { this.currentTime = new Date() }      , 1000);  }}

登录后复制

test-pipe.component.html

    
管道的用法
    
      {{ currentTime | date:'yyyy-MM-dd HH:mm:ss' }}    
    
      {{ str | uppercase }}    
    
      {{ money | number: '2.2-2' }}    

登录后复制

非空断言

test-not-null-assert.component.ts

@Component({  selector: 'app-test-safe-nav',  templateUrl: './test-not-null-assert.component.html',  styleUrls: ['./test-not-null-assert.component.css']})export class TestSafeNavComponent implements OnInit {  public currentValue: any = null;  constructor() { }  ngOnInit() {  }}

登录后复制test-not-null-assert.component.html

  
安全取值
  
    名字:{{currentValue?.name}}  

登录后复制

更多编程相关知识,可访问:编程教学!!

以上就是详解Angular中的模板语法的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2714176.html

(0)
上一篇 2025年3月7日 21:31:33
下一篇 2025年2月18日 11:39:13

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 详解Angular中组件间通讯的几种方法

    本篇文章带大家详细了解一下angular中组件间通讯的几种。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular 组件间的通讯 组件间三种典型关系: 父好组件之间的交互(@Input/@Output/模板变量/@…

    2025年3月7日
    200
  • 深入了解Angular组件中的生命周期钩子

    本篇文章带大家了解一下angular组件生命周期钩子。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular 组件生命周期钩子 其中,红色标记的生命周期钩子只调用一次,绿色部分会被反复调用,执行顺序依次由上而下。 …

    2025年3月7日
    200
  • 深入了解Angular中的表单

    本篇文章给大家详细介绍一下angular中的表单。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular 表单 什么是模板式表单 表单的数据模型是通过组件模板中的相关指令来定义的, 因为使用这种方式定义表单的数据模…

    2025年3月7日
    200
  • 详解Angular使用ControlValueAccessor实现自定义表单控件

    本篇文章给大家介绍一下angular使用controlvalueaccessor实现自定义表单控件的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular: [ControlValueAccessor] 自定…

    2025年3月7日
    200
  • 详解Angular中自定义创建指令的方法

    本篇文章给大家介绍一下在 angular 中如何自定义创建指令。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 指令介绍 在 Angular 中有三种类型的指令: 组件,有模板的指令,组件是继承于指令的,只是扩展类与 UI…

    2025年3月7日
    200
  • 2021年值得尝试的7个Angular前端组件库,快来收藏吧!

    Angular 是一款能够跨 Web、移动 Web、移动应用、原生应用和桌面原生应用多个平台的前端框架,经过数十年的发展,已形成了一个庞大的生态,基于Angular的组件库也是多如牛毛。 2021年如果你想尝试 Angular 框架,以下 …

    2025年3月7日 编程技术
    200
  • 详解Angular中的模板输入变量(let-变量)

    本篇文章带大家了解一下angular中的模板输入变量(let-变量)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 我这个人,写文章或者说心得,不喜欢直接抄官网上面的东西,实在是没啥意思。我还是喜欢用我的大白话来写文章。…

    2025年3月7日 编程技术
    200
  • 详解Angular中的组件交互

    本篇文章带大家详细介绍一下angular组件交互。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular 组件交互 组件交互: 组件通讯,让两个或多个组件之间共享信息。使用场景: 当某个功能在多个组件中被使用到时,…

    2025年3月7日
    200
  • 浅谈angular中@、=、&指令的差异

    本篇文章带大家了解一下angular指令中@,=,&的区别。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 【相关推荐:《angular教程》】 当directive中的scope设置为一个对象的时候,该指令就有了…

    2025年3月7日
    200
  • Angular如何创建服务?5种方式了解一下!

    本篇文章给大家介绍一下angular创建服务的5种方式。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 config配置块 Angular应用的运行主要分为两部分:app.config()和app.run(),config…

    2025年3月7日
    200

发表回复

登录后才能评论