Angular模板语法

news2024/9/21 3:23:10

1 Angular数据文本绑定

Angular 中使用{{}}绑定业务逻辑里面定义的数据
<div class="title"> {{title}}</div>

2 Angular模板里面绑定属性

[title]="student"绑定动态属性

ts:
public title: string = 'zhaoshuai-lc'
html:
<div [title]="student">
    张三
</div>

3 Angular模板里面绑定Html

ts:
public content = "<h2>i am h2</h2>"
html:
<span [innerHTML]='content' class="red"></span>

4 Angular模板里面允许做简单的运算

1+2={{1+2}}

5 数据循环 *ngFor

public arr: Array<string> = ['one', 'two', 'three', 'four']

<!--*ngFor 普通循环-->
<ul>
  <li *ngFor="let item of arr">{{item}}</li>
</ul>
  public userInfoList: Array<any> = [{
    name: 'zhaoshuai-lc',
    age: 10
  }, {
    name: 'zhaoyushuai-lc',
    age: 12
  }, {
    name: 'duanshuxian',
    age: 13
  }]
  
<ul>
  <li *ngFor="let item of userInfoList">
    {{item.name}} --- {{item.age}}
  </li>
</ul>
 public newsList: Array<any> = [
   {
     title: 'news-1'
   },
   {
     title: 'news-2'
   },
   {
     title: 'news-3'
   }
 ]

<!--循环的时候设置 key-->
<ul>
  <li *ngFor="let item of newsList; let key = index">
    {{key}} -- {{item.title}}
  </li>
</ul>

6 条件判断语句 *ngIf

<!--条件判断语句 *ngIf-->
<div *ngIf="flag">
  i am first
</div>
<div *ngIf="!flag">
  i am second
</div>

7 条件判断语句 *ngSwitch

public orderStatus: number = 10

<!--条件判断语句 *ngSwitch-->
<span [ngSwitch]="orderStatus">
  <p *ngSwitchCase="1">1</p>
  <p *ngSwitchCase="2">2</p>
  <p *ngSwitchCase="3">3</p>
  <p *ngSwitchDefault>default</p>
</span>

8 属性[ngClass]

.define-style {
  color: red;
}

.green-style {
  color: green;
}

.yellow-style {
  color: yellow;
}
<!--属性[ngClass]-->
<div class="define-style">
  ngClass演示 (尽量不要用dom来改变class</div>

<div [ngClass]="{ 'green-style': false, 'yellow-style': true}">
  ngClass演示
</div>

<div [ngClass]="{'green-style': flag, 'yellow-style': !flag}">
  [ngClass]演示
</div>

<strong>循环数组,让数组的第一个元素的样式为red ,第二个为orange,第三个为blue</strong>
<ul>
  <li *ngFor="let item of newsList; let key=index;"
      [ngClass]="{'green-style': key==0,'yellow-style': key==1, 'define-style': key==2}">
    {{key}}---{{item.title}}
  </li>
</ul>

9 属性[ngStyle]

public attr: string = 'green'

<!--属性[ngStyle]-->
<p style="color:red">我是一个p标签</p>
<p [ngStyle]="{'color':'blue'}">我是一个p标签</p>
<p [ngStyle]="{'color': attr}">我是一个p标签</p>

10 管道

public today: any = new Date()

<!--管道-->
today: {{ today }}
<br/>
-today: {{today | date:'yyyy-MM-dd HH:mm:ss'}}

11 执行事件(click)

<button (click)="run()">执行事件</button>
<button (click)="getData()">执行方法获取属性里面的数据</button>
<div>{{ username1 }}</div>
<button (click)="setData()">执行方法改变属性里面的数据</button>
public username1: any = 'zhaoshuai-lc'

  run() {
    alert("run function")
  }

  getData() {
    alert(this.username1)
  }

  setData() {
    this.username1 = 'setData-username1'
  }

12 表单事件-事件对象

<input type="text" (keydown)="keyDown()"/>
<input type="text" (keydown)="_keyDown($event)"/>
  keyDown() {
    alert("keyDown")
  }
  _keyDown(e) {
    console.log(e.target.value)
  }

13 双向数据绑定

public keywords: string = ''
changeKeywords() {
  this.keywords = 'changeKeywords-keywords'
}

<!--双向数据绑定–MVVM 只是针对表单-->
<input type="text" [(ngModel)]='keywords' />
{{keywords}}
<button (click)="changeKeywords()">改变keywords的值</button>

14 ViewChild的用法

Angular的ViewChild用法是用来获取模板视图中的元素或组件的引用。

<app-component-module #componentModuleComponent></app-component-module>
import {Component, ViewChild} from '@angular/core';
import { ComponentModuleComponent } from './components/component-module/component-module.component'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {
  title = 'firstProject';

  public parentValue: string = 'parentValue'

  // ViewChild 组件实例调用 父调用子组件
  @ViewChild(ComponentModuleComponent) componentModuleComponent: any;
  @ViewChild('componentModuleComponent') _componentModuleComponent: any;

  // 简单来说就是当组件视图渲染完成后调用,也就是说在这个函数执行之前是没有办法获取到HTML里面的内容的
  ngAfterViewInit(): void {
    console.log(this.componentModuleComponent);
    console.log(this._componentModuleComponent);
  }


}

在这里插入图片描述

15 引入服务

  1. 创建一个服务文件:在你的Angular项目中,使用以下命令创建一个新的服务文件。
ng g service your-service-name

这将在你的项目目录中创建一个名为your-service-name.service.ts的文件,并自动生成基本的Angular服务代码。

  1. 编写服务代码:在your-service-name.service.ts文件中,你可以编写服务的方法和属性。例如,你可以定义一些用于获取数据的方法,或者创建一个用于管理用户状态的服务的属性。
import {Injectable} from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class StorageService {

  constructor() {
  }

  set(key: string, value: any) {
    localStorage.setItem(key, JSON.stringify(value))
  }

  get(key: string) {
    return localStorage.getItem(key)
  }
}
  1. 使用服务:你可以在任何组件中注入服务并使用它。在组件的构造函数中,使用@Inject装饰器来注入服务。
import { Component } from '@angular/core';  
import { YourServiceNameService } from './your-service-name.service';  
  
@Component({  
  selector: 'app-your-component',  
  templateUrl: './your-component.component.html',  
  styleUrls: ['./your-component.component.css'],  
})  
export class YourComponent {  
  constructor(private yourService: YourServiceNameService) {}  
  
  // 在这里使用你的服务  
}
  1. 在模板中使用服务:一旦你在组件中注入了服务,你就可以在模板中使用它。例如,你可以使用服务的方法来获取数据并在模板中显示它们。
<div *ngIf="yourService.getData()">  
  <p>{{ yourService.getData() }}</p>  
</div>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1163669.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

自制目录扫描工具并由py文件转为exe可执行程序

心血来潮让ChatGPT写了一个目录扫描工具&#xff0c;然后进行了一定的修改和完善&#xff0c;可以实现对网站目录的一个简单扫描并输出扫描结果&#xff0c;主要包括存在页面、重定向页面和禁止访问页面。 虽然代码很简单&#xff0c;但是做这个东西的过程还是挺有意思的&…

Jmeter只能做性能测试吗?

Jmeter除了可以性能测试&#xff0c;还能做接口测试 1、Jmeter和Fiddler&#xff0c;Postman有什么区别? Fiddler&#xff1a;虽然有接口测试功能&#xff0c;很少用来做接口测试。 一般用Fiddle来做抓包和异常测试&#xff0c;辅助接口测试。Postman&#xff1a; 是接口调试…

求任意连续子段的最小值之和

一道超级经典的单调栈问题&#xff0c;本题的关键在于你不要同时两边取等号&#xff0c;不然相等的区间会重复计算 还有记得开long long #include<bits/stdc.h> using namespace std; using lllong long; const int N 2e510; int n; ll a[N]; ll l[N]; ll r[N]; int m…

cudnn too short

原因是libcudnn.so为软链接&#xff0c;相当于快捷键&#xff0c;但是没有映射到真正的libcudnn.so.8.9.5上 cd /usr/local/cuda-11.6/lib64 ln -s libcudnn.so.8.9.5 libcudnn.so.8 ln -s libcudnn.so.8.9.5 libcudnn.so

VMware安装RedHat8.3虚拟机

red hat enterprise linux 8.3 ios镜像 链接&#xff1a;https://pan.baidu.com/s/1HbgXTh8q_YWlVVs8VAa14g?pwdot10 提取码&#xff1a;ot10 Red Hat Enterprise Linux&#xff08;RHEL&#xff09; 是一款由红帽公司开发和支持的商业操作系统。RHEL 8.3是RHEL 8系列的一个…

数据库事务提交后才发送MQ消息解决方案

项目场景&#xff1a; 在项目开发中常常会遇到在一个有数据库操作的方法中&#xff0c;发送MQ消息&#xff0c;如果这种情况消息队列效率比较快&#xff0c;就会出现数据库事务还没提交&#xff0c;消息队列已经执行业务&#xff0c;导致不一致问题。举个应用场景&#xff0c;…

stm32 ADC

目录 简介 stm32的adc 框图 ①电压输入范围 ②输入通道 ​编辑③ADC通道 ④ADC触发 ⑤ADC中断 ⑥ADC数据 ⑦ADC时钟 ADC的四种转换模式 hal库代码 标准库代码 简介 自然界的信号几乎都是模拟信号&#xff0c;比如光亮、温度、压力、声音&#xff0c;而为了方便存储、…

容器:软件性能测试的最佳环境

容器总体上提供了一种经济的和可扩展的方法来测试产品在实际情况下的性能&#xff0c;同时还能保持较低的资源成本和开销成本。 软件性能和可伸缩性是我们谈论应用程序开发时经常遇到的话题。一个很大的原因是应用程序的性能和可伸缩性直接影响其在市场上的成功。一个应用程序…

搭建Qt5.7.1+kylinV10开发环境、运行环境

1.下载Qt源码 Index of / 2.编译Qt 解压缩qt-everywhere-opensource-src-5.7.1.tar.gz 进入到qt-everywhere-opensource-src-5.7.1/qtbase/mkspecs这个目录下&#xff0c; 2.1找到以下目录 复制他&#xff0c;然后改名linux-x86-arrch64&#xff0c;博主这里名字取的有些问…

历年网规上午真题(2017年)

解析:D/C 计算机主要性能指标:时钟频率(主频)、运算速度、运算精度、内存大小、数据处理速率(PDR)等 数据库主要指标:最大并发、负载均衡能力、最大连接数等 解析:A 敏捷开发是一种应对快速变化的需求的一种软件开发方法,是一种以人为核心、迭代、循序渐进的开发方…

深度学习之基于Yolov5闯红灯及红绿灯检测系统

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 文章目录 一项目简介 二、功能三、闯红灯及红绿灯检测系统![请添加图片描述](https://img-blog.csdnimg.cn/8f260c2ed5ed4d8596e27d38abe42745.jpeg)四. 总结 一项目简介 基于Y…

生成独立运行的QT程序

前言 使用windeployqt程序生成独立运行的QT程序。 方法 1.在QT Creator使用release构建运行一下代码&#xff0c;不使用debug模式&#xff0c;将release文件夹中生成的***.exe文件复制到一个新的文件夹下。 2.打开 Qt 5.14.2(MinGW 7.3.0 64-bit) 进入exe文件所在的目录执…

Java精品项目62基于Springboot+Vue实现的大学生在线答疑平台(编号V62)

Java精品项目62基于SpringbootVue实现的大学生在线答疑平台(编号V62) 大家好&#xff0c;小辰今天给大家介绍一个基于SpringbootVue实现的大学生在线答疑平台(编号V62)&#xff0c;演示视频公众号&#xff08;小辰哥的Java&#xff09;对号查询观看即可 文章目录 Java精品项目…

Temu新规,12岁以下儿童产品必需提供CPC认证

近日Temu发布儿童用品合规通知&#xff1a;为保障Temu平台消费者的合法权益&#xff0c;保障儿童类商品在目的国的正常销售及合规要求&#xff0c;针对以12岁及以下儿童为主要使用对象的产品&#xff0c; 卖家需提供CPC认证&#xff0c;要求说明&#xff1a; 1、所有产品均需…

linux 下 物理迁移 mysql 数据库 不能启动问题

1、授权问题 # chown -R 777 /app/db/mysql 2、/etc/my.cnf配置问题 [mysqld] basedir/app/db/mysql datadir/app/db/mysql/data socket/app/db/mysql/mysql.sock.lock innodb_buffer_pool_size128M innodb_force_recovery 1 symbolic-links0 [mysqld_safe] log-error/app/…

半方差函数详解

1 引言 托布勒的地理第一定律指出&#xff0c;“一切都与其他事物有关&#xff0c;但近处的事物比远处的事物更相关。 在半变异函数的情况下&#xff0c;更接近的事物更可预测&#xff0c;变异性更小&#xff0c;而遥远的事物则难以预测&#xff0c;相关性也较低。 例如&…

做CSGO游戏搬砖前,这五个问题必须了解

相信经常看我文章的人或多或少都已经了解steam搬砖项目&#xff0c;也叫CSGO游戏搬砖项目&#xff0c;还有人叫它&#xff1a;国外steam游戏汇率差项目&#xff0c;无论怎么称呼&#xff0c;都是同一个项目。虽然我已经实操Steam搬砖项目超过三年&#xff0c;带领了数百名学员踏…

文字间隔css

文字间隔 letter-spacing: 3px

linux入门---线程的同步

目录标题 什么是同步生产者和消费者模型三者之间的关系消费者生产者模型改进生产者消费者模型特点条件变量的作用条件变量有关的函数条件变量的理解条件变量的使用 什么是同步 这里通过一个例子来带着大家了解一下什么是同步&#xff0c;在生活中大家肯定遇到过排队的情景比如…