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 (四)第五节: ...
随机推荐
- c#接口与虚函数的实验报告
1)定义Student类,用string型变量name存储学生姓名,用int型变量age存储学生年龄.Student类实现IComparable接口.要求从键盘输入学生的姓名和年龄,并注意可能出现的异 ...
- 记录一次追查server死机& 登录异常情况
linux 服务器死机了,于是追查原因. 查看boot.log wade@koala:/var/log$ less boot.log 看不出异常 显示开机信息 wade@koala:/var/log$ ...
- SpringJDBC——jdbcTemplate模板
一.定义 Spring框架对jdbc进行了封装,提供的一个JDBCTemplated对象简化jdbc开发. 使用步骤 1 导包spring-beans-5.0.0-RELEASE.jar,spring ...
- Android资源收集
1 android studio最新版本下载地址:https://developer.android.google.cn/studio/preview/?utm_source=android-stud ...
- Exclude the folders/files for indexing
如果你的项目有非常多的文件,目录,Eclipse 有一个很好的Resource Filter 可以把有某些特征的文件,目录不再进行索引.
- 利用 groupby apply list 分组合并字符
利用 groupby apply list 分组合并字符 因为需要对数据进行分组和合并字符,找到了以下方法. 有点类似 SQL 的 Group BY. import pandas as pd impo ...
- redis 学习资料
redis 学习资料 网址 Redis 教程(菜鸟教程) http://www.runoob.com/redis/redis-tutorial.html Redis 命令参考 http://redis ...
- 新版vue-cli输入本地ip不能访问,只能用localhost才可以访问?
问:新版vue-cli输入本地ip不能访问,只能用localhost才可以访问? 答:修改config/index.js配置,将host: 'localhost',改为host: '0.0.0.0', ...
- 基于mysql对mybatis中的foreach进行深入研究
鉴于上一篇博文一次修改mysql字段类型引发的技术探究提到的,要对foreach里面的collection相关的内容做一些介绍,今天就围绕foreach,做一些数据插入和查询相关的研究. 首先介绍一下 ...
- PHP遍历一个文件夹下所有文件和子文件夹的函数
<?php function my_dir($dir) { $files = array(); if(@$handle = opendir($dir)) { //注意这里要加一个@,不然会有wa ...