angular的subscribe
angular中可以使用observable和subscribe实现订阅,从而实现异步。
这里记录一个工作中的小问题,以加深对subscribe的理解。前端技能弱,慢慢积累中。
本来希望的是点击一个按钮后出现一个loading的模态框,实际发现并没有出现loading的模态框。
按钮和模态框的代码,点击删除按钮后,出现模态框,删除按钮消失
<div *ngIf = "rotateState === 0">
loading 模态框
</div>
<div *ngIf = "rotateState === 1">
<button (click) = "delete()">删除</button>
</div>
delete方法的代码:
delete(id: any){
this.rotateState =0;
this.deleteInterface(id).subscribe(res =>{
...
},error=>{
...
})
}
deleteInterface是调用后台restful接口的一个方法,返回的是一个observable对象。
测试发现第一次删除是好的,有模态框显示,但是后来就不会出现删除按钮了,于是在代码的最后加上了this.rotateState =1;
delete(id: any){
this.rotateState =0;
this.deleteInterface(id).subscribe(res =>{
...
},error=>{
...
})
this.rotateState =1;
}
这样每次点开有删除按钮,模态框却没有了。
observable和subscribe是异步的,点击按钮触发delete方法后,不会等待restful的调用,执行this.rotateState =1就不会显示等待的模态框了。
delete(id: any){
this.rotateState =0;
this.deleteInterface(id).subscribe(res =>{
...
this.rotateState =1;
},error=>{
...
this.rotateState =1;
})
}
这样就可以保证多次打开后都有按钮,并且有等待的模态框。
angular的subscribe的更多相关文章
- 对Promise的一些深入了解
1.介绍promise和模仿Promise.all和Promise.race promise的设计主要是解决回调地狱(接收结果用回调函数来处理,但必须传入回调函数)的问题,由一层层嵌套回调函数改为由t ...
- TypeScript: Angular 2 的秘密武器(译)
本文整理自Dan Wahlin在ng-conf上的talk.原视频地址: https://www.youtube.com/watch?v=e3djIqAGqZo 开场白 开场白主要分为三部分: 感谢了 ...
- 后端学 Angular 2 —— 组件间通信
1. 父组件向子组件传递信息 使用@Input 子组件的属性用 @Input 进行修饰,在父组件的模板中绑定变量 例子: import { Component, OnInit, Input } fro ...
- Angular数据双向绑定
Angular数据双向绑定 AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.Angul ...
- [Angular2 Router] Exiting an Angular 2 Route - How To Prevent Memory Leaks
In this tutorial we are going to learn how we can accidentally creating memory leaks in our applicat ...
- [Angular2 Form] Use RxJS Streams with Angular 2 Forms
Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of t ...
- Redux你的Angular 2应用--ngRx使用体验
Angular2和Rx的相关知识可以看我的Angular 2.0 从0到1系列第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2 ...
- Angular 2.0 从0到1:Rx--隐藏在Angular 2.x中利剑
第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...
- Angular 2.0 从0到1 (七)
第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...
随机推荐
- hdu4336 Card Collector 容斥原理
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- Architecture options to run a workflow engine
This week a customer called and asked (translated into my own words and shortened): “We do composite ...
- lapis 项目添加prometheus 监控
lapis 是基于openresty 扩展的,所以直接将支持prometheus的模块构建进openresty 就可以了 我使用的是nginx-module-vts 模块 环境准备 我已经构建好了 ...
- 使用patroni 解决hasura graphql-engine pg 数据库ha的问题
环境准备 机器pg 数据库地址修改为haproxy 的ip地址,端口是haproxy的tcp 端口,配置比较简单 hasura graphql-engine docker-compose versio ...
- 02基于python玩转人工智能最火框架之TensorFlow人工智能&深度学习介绍
人工智能之父麦卡锡给出的定义 构建智能机器,特别是智能计算机程序的科学和工程. 人工智能是一种让计算机程序能够"智能地"思考的方式 思考的模式类似于人类. 什么是智能? 智能的英语 ...
- 动态quartz新增、修改、删除
1.定义job导出类 public class MyQuartzJob implements Job { private static Logger logger = LoggerFactory.ge ...
- jquery遍历table的tr获取td的值
方法一: var siginArray = []; $("#tbody").children("tr").each(function () { var sigi ...
- Feign 自定义编码器、解码器和客户端
Feign 的编码器.解码器和客户端都是支持自定义扩展,可以对请求以及结果和发起请求的过程进行自定义实现,Feign 默认支持 JSON 格式的编码器和解码器,如果希望支持其他的或者自定义格式就需要编 ...
- Linux命令之sed
sed命令格式 sed [options] 'command' file(s) 选项 -e :直接在命令行模式上进行sed动作编辑,此为默认选项; -f :将sed的动作写在一个文件内,用–f fil ...
- [2]注解(Annotation)-- 深入理解Java:注解(Annotation)自定义注解入门
转载 http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html 深入理解Java:注解(Annotation)自定义注解入门 要深入学习 ...