Angular旋转动画隐藏函数不起作用
创始人
2024-10-30 10:01:02
0次

问题描述:

在Angular的应用程序中使用旋转动画(Spinner),可能会遇到一个问题,隐藏函数在调用时没有生效。

例如:

component.ts文件中:

showSpinner = false;

hideSpinner() {
   this.showSpinner = false;
}

component.html文件中:

在这种情况下,隐藏函数调用后,旋转动画仍然继续显示。

解决方案:

这个问题的解决方案是将旋转动画的显示和隐藏控制放到共享服务中。

例如:

loader.service.ts文件中:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({
   providedIn: 'root'
})
export class LoaderService {
   public isLoading: BehaviorSubject = new BehaviorSubject(false);
   constructor() { }
   show() {
      this.isLoading.next(true);
   }
   hide() {
      this.isLoading.next(false);
   }
}

component.ts文件中:

import { Component } from '@angular/core';
import { LoaderService } from './loader.service';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   constructor(private loaderService: LoaderService) {}
   showLoadingIndicator() {
      this.loaderService.show();
      setTimeout(() => {
         this.loaderService.hide();
      }, 3000);
   }
}

component.html文件中:


这个解决方案中,showLoadingIndicator函数调用show函数来显示旋转动画,并在3秒钟后调用hide函数来隐藏动画。

此外,在component.html文件中,使用Async管道订阅LoaderService的isLoading属性,根据其值来控制动画状态。

最后,应确保在AppModule中

相关内容

热门资讯

一分钟揭秘!微乐小程序免费黑科... 一分钟揭秘!微乐小程序免费黑科技(开挂),微乐浙江麻将原来真的是有透视挂工具微乐小程序免费黑科技能透...
记者爆料!微乐小程序免费黑科技... 记者爆料!微乐小程序免费黑科技(外挂),微乐小程序竟然有透视挂脚本1、进入到微乐小程序免费黑科技是否...
热点推荐!微乐自建房怎么提高运... 热点推荐!微乐自建房怎么提高运气(外挂),微乐锄大地好像是有透视挂软件1、完成微乐自建房怎么提高运气...
推荐十款!微乐自建房脚本入口(... 推荐十款!微乐自建房脚本入口(外挂),微乐炸红十一贯是真的有透视挂技巧进入游戏-大厅左侧-新手福利-...
今日公布!微乐兰州麻将小程序辅... 今日公布!微乐兰州麻将小程序辅助(作弊器),微乐三打一原来是真的有透视挂插件1)微乐兰州麻将小程序辅...
一分钟揭秘!微乐小程序免费黑科... 一分钟揭秘!微乐小程序免费黑科技是真的吗(辅助器),微乐刨幺填大坑果然真的有透视挂教程1、很好的工具...
分享一款!微乐小程序免费脚本(... 分享一款!微乐小程序免费脚本(外挂),微乐卡五星一贯真的是有透视挂技巧1、该软件可以轻松地帮助玩家将...
玩家必看分享!微乐小程序黑科技... 玩家必看分享!微乐小程序黑科技辅助工具推荐(开挂),微乐捉鸡麻将果然是有透视挂脚本1、全新机制【微乐...
安装程序教程!微乐小程序辅助器... 安装程序教程!微乐小程序辅助器脚本(作弊器),微乐河南红中麻将本来有透视挂app所有人都在同一条线上...
技术分享!微乐小程序辅助开挂(... 技术分享!微乐小程序辅助开挂(辅助),微乐河北麻将一直有透视挂插件1、微乐小程序辅助开挂模拟器是什么...