Angular7教程-06-页面与数据交互
1. 本节说明
本节的内容会在上期搭建的框架基础上进行数据的填充,顺便回顾之前介绍过的插值表达式,属性绑定等知识,本节的数据只是在组件中模拟数据,后面会有专门的章节讲解如何从服务器获取数据。
2. 轮播组件属性绑定
首先把轮播图使用的图片放在项目的src/assets目录下(图片请自行准备),然后在carousel.component.ts中定义轮播使用的图片属性:
import { Component, OnInit } from '@angular/core';
@Component({
	selector: 'app-carousel',
	templateUrl: './carousel.component.html',
	styleUrls: ['./carousel.component.css']
})
export class CarouselComponent implements OnInit {
	//step2.定义三张图片
	private img1:Img;
	private img2:Img;
	private img3:Img;
	constructor() { }
	//step3.然后初始化图片
	ngOnInit() {
		this.img1 = new Img("../assets/1.jpg","图片一");
		this.img2 = new Img("../assets/2.jpg","图片二");
		this.img3 = new Img("../assets/3.jpg","图片三");
	}
}
//step1.定义轮播的图片对象
export class Img {
	constructor(
		public imgSrc: String,
		public imgAlt: String
	) {
	}
}
carousel.component.html修改如下:
<div id="carousel-ex" class="carousel slide" data-ride="carousel">
	<ol class="carousel-indicators">
		<li data-target="carousel-ex" data-slide-to="0" class="active"></li>
		<li data-target="carousel-ex" data-slide-to="1"></li>
		<li data-target="carousel-ex" data-slide-to="2"></li>
	</ol>
	<div class="carousel-inner listbox">
		<div class="item active">
            <!-- 属性绑定 -->
			<img [src]="img1.imgSrc" [alt]="img1.imgAlt">
			<div class="carousel-caption">
				{{img1.imgAlt}}
			</div>
		</div>
		<div class="item">
			<img [src]="img2.imgSrc" [alt]="img2.imgAlt">
			<div class="carousel-caption">
				{{img2.imgAlt}}
			</div>
		</div>
		<div class="item">
			<img [src]="img3.imgSrc" [alt]="img3.imgAlt">
			<div class="carousel-caption">
				{{img3.imgAlt}}
			</div>
		</div>
	</div>
	<a href="#carousel-ex" class="left carousel-control" role="button" data-slide="prev">
		<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
		<span class="sr-only">Previous</span>
	</a>
	<a href="#carousel-ex" class="right carousel-control" role="button" data-slide="next">
		<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
		<span class="sr-only">Next</span>
	</a>
</div>
页面效果如下:

3.文章组件数据循环
首先修改article.component.ts初始化文章数据:
import { Component, OnInit } from '@angular/core';
@Component({
	selector: 'app-article',
	templateUrl: './article.component.html',
	styleUrls: [
		'./article.component.css'
	]
})
export class ArticleComponent implements OnInit {
	//step2.声明文章对象数组
	private articles: Array<Article>;
	constructor() {
	}
	//step3.初始化数组数据
	ngOnInit() {
		this.articles = [
			new Article(1,"angular常用操作1","admin","本节介绍angular常用操作...",3000,50),
			new Article(2,"angular常用操作2","admin","本节介绍angular常用操作...",600,10),
			new Article(3,"angular常用操作3","admin","本节介绍angular常用操作...",20,5),
		]
	}
}
//step1. 定义文章对象
export class Article{
	constructor(
		public id: number,		//文章Id
		public title: String,	//文章标题
		public author: String,	//文章作者
		public zy: String,		//文章摘要
		public yd: number,		//阅读数
		public pl: number		//评论数
	){
	}
}
然后修改article.component.html 内容如下:
<div class="content-wrap">
	<div *ngFor="let article of articles" class="article">
		<h3 class="title">{{article.title}}</h3>
		<p class="zy">
			{{article.zy}}
		</p>
		<p class="info">
			<span>2018-11-18 21:15:</span>  
			<span>阅读数:{{article.yd}}</span>  
			<span>评论数:{{article.pl}}</span>  
		</p>
	</div>
</div>
页面效果如下所示:

4. 样式绑定的另外一种方法
现在实现这样一个需求,当文章的阅读量超过1000时,文章的标题以红色显示。
首先,我们在article.component.css中增加样式:
.hot{
	color: red !important;
}
然后在article.component.html中需要添加样式的地方添加如下代码:
<!-- 当article.yd>1000时,h3会加上hot样式,否则不加 -->
<h3 class="title" [class.hot]="article.yd>1000">{{article.title}}</h3>
页面效果如下所示:

Angular7教程-06-页面与数据交互的更多相关文章
- php文件与HTML页面的数据交互
		注意:首先需要保证本地配置了php开发环境,如WAMP开发环境 WAMP配置:https://www.cnblogs.com/shiyiaccn/p/9984579.html php获取HTML页面返 ... 
- 关于AJAX+HTML5+ASHX进行全静态页面的数据交互
		及时总结项目中使用到的知识,知识在于积累. 1.HTML代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ... 
- 无废话ExtJs 入门教程二十[数据交互:AJAX]
		无废话ExtJs 入门教程二十[数据交互:AJAX] extjs技术交流,欢迎加群(521711109) 1.代码如下: 1 <!DOCTYPE html PUBLIC "-//W3C ... 
- H5页面基于接口实现数据交互
		对于现在APP开发来说,目前流行的两个方式是原生和H5.就如同之前业界程序猿争论的BS和CS之争一样,业界对于H5和原生也有不小的争论.对于前者的争论在于PC端,后者在于移动端上体现. 那一个APP适 ... 
- QtQuick多页面切换、多页面切换动画、多个qml文件数据交互
		一.QtQuick多页面切换方法 (1)“隐藏法” 前一个视图visible设为false或者透明度opacity设为0,相当于“隐藏”了,实际还存在: 要显示的视图visible设为true或者透明 ... 
- MUI框架-10-MUI 数据交互-跳转详情页面
		MUI框架-10-MUI 数据交互-跳转详情页面 上一篇介绍了如何实现数据交互,给别人的 API 发送 ajax 请求,我们得到数据,再使用 art-template 模板引擎拼接 HTML,最终实现 ... 
- SpringMVC4+thymeleaf3的一个简单实例(篇五:页面和MySql的数据交互-展示以及存储)
		这一篇将介绍怎样把页面数据保存的MySQL数据库,并将数据库内容展示到页面上.首先做一个基础工作,添加以下jar到lib:1: mysql-connector-Java-5.1.40-bin.jar ... 
- 《ServerSuperIO Designer IDE使用教程》- 6.增加与阿里云物联网(IOT)对接服务,实现数据交互。发布:v4.2.4 版本
		v4.2.4 更新内容:1.增加了对接阿里物联网平台的服务.下载地址:官方下载 6. 增加与阿里云物联网(IOT)对接服务,实现数据交互 6.1 概述 为了满足业务系统数据上云的要求,Se ... 
- 《ServerSuperIO Designer IDE使用教程》-2.与硬件网关数据交互,并进行数据级联转发,直到云端。发布:v4.2.1版本
		v4.2.1 更新内容:1.重新定义数据转发文本协议,使网关与ServerSuperIO以及之间能够相关交互数据.2.扩展ServerSuperIO动态数据类的方法,更灵活.3.修复Designer增 ... 
随机推荐
- iview动态校验表单,获取值为undefined
			场景:实际代码如下:https://run.iviewui.com/XPofr3YS 原因:在动态校验名称时,没法获取值,请教了大神后,发现原来是自己demo没理清楚 这里的prop="na ... 
- git基础命令。
			创建一个新的本地仓库 git init 克隆远程服务器 git clone URL 打开一个文件夹 cd 添加文件 git add . 本地提交更改 git commit -m ... 
- Reporting Service 2005 迁移 到 2008
			1. 备份ReportServer 和ReportServerTempDB 的数据库. 2. 在Reporting Services Configuration Manager 中备份Encrypti ... 
- Word 最后一页无法删除-解决办法
			Word 最后一页无法删除-解决办法 制服 word 最后一页无法删除 今天在做一个简历的时候,编辑 word 文档的时候,最后一页空白页怎么也删不掉,百度了很多方法之后,只有一个可行,记录一下. 1 ... 
- Vue小案例(一)
			案例需求: 创建一个品牌展示表格,表头有编号(id),品牌名称(name),创建时间(time)和操作,需要实现的功能是对数据的增删操作,和时间的格式化. 思路分析:在开发之前需要想清楚要用到Vue中 ... 
- CPU硬件辅助虚拟化技术
			目前主要有Intel的VT-x和AMD的AMD-V这两种技术.其核心思想都是通过引入新的指令和运行模式,使VMM和Guest OS分别运行在不同模式(ROOT模式和非ROOT模式)下,且Guest O ... 
- 远程桌面到 Ubuntu 虚拟机
			安装 Ubuntu 虚拟机,创建端口号为 3389 的 Endpoint. 安装 Gnome 桌面 复制 sudo apt-get update sudo apt-get install ubuntu ... 
- 对于当下国产CPU如火如荼有感
			国家在国家战略层面去做国产CPU这个事情,从初衷来说是好的.国产CPU战略如果能够实现,则会大大加强我国在计算机产业领域从头到尾的话语权与技术竞争力.但是个人觉得,事情不是那么简单.我将从下面几个方面 ... 
- Python学习---JSON学习180130
			JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON是用字符串来表示Javascript对象: Json字符串就是js对象的一种表现形式(字符串的形式 ... 
- Linux ping命令详解
			Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性 基于IMCP协议 常见命令参数 -q 不显示任何传送封包的信息,只显示最后的结果 -n 只输出数值 -R 记录路由过程 ... 
