这可能是因为我通常误解了Angular,因为我目前只是在玩它.我试图使用"Angular /material /进度条"组件显示虚假的进度条.(https://material.angular.io/components/progress-bar/)

我可以在"app.component.html"中显示这样的进度条:

<mat-progress-bar id="progBar1" mode="determinate" [value]= "progressBarValue" class="visible"></mat-progress-bar>

我还可以使用"app.component.ts"中的"Value"输入来设置其进度一次.但是,如果我使用setSYS()或setInterval()来延迟更改或重复添加未应用的值:

import { Component } from '@angular/core';
import {MatProgressBar, MatProgressBarModule} from '@angular/material/progress-bar'; 

var progressBarValueInternal:number = 1;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrl: './app.component.css'
})
export class AppComponent {
  title = 'Eduardeo-App';
  progressBarValue = progressBarValueInternal;
}

progressBarValueInternal=50;
setTimeout(()=>{console.log("Setting ProgressBar to 75"); progressBarValueInternal=75;}, 5000);

使用上述代码,控制台将打印"将ProgressBar设置为75",但该Bar将保持在最初设置的50%.有什么 idea 吗?为什么会是这样?

推荐答案

所有初始化操作都需要在ngOnInit挂钩上发生

Angular component life cycle docs

import { Component } from '@angular/core';
import {MatProgressBar, MatProgressBarModule} from '@angular/material/progress-bar'; 

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrl: './app.component.css'
})
export class AppComponent {
  title = 'Eduardeo-App';
  progressBarValue = 1;
  progressBarValueInternal=50;

  ngOnInit(){
      setTimeout(()=>{
          console.log("Setting ProgressBar to 75"); 
          this.progressBarValueInternal=75;
      }, 5000);
  }
}

Javascript相关问答推荐

如何在react + react路由域名中使用DeliverBrowserRouter?

如何在alpinejs中显示dev中x-for的元素

我不知道为什么我的JavaScript没有验证我的表单

TypeScript索引签名模板限制

编剧如何获得一个div内的所有链接,然后判断每个链接是否都会得到200?

优化Google Sheet脚本以将下拉菜单和公式添加到多行

Angular 订阅部分相互依赖并返回数组多个异步Http调用

如何将未排序的元素追加到数组的末尾?

从另一个数组中的对应行/键值对更新数组中的键值对对象

try 使用PM2在AWS ubuntu服务器上运行 node 进程时出错

背景动画让网站摇摇欲坠

Node.js API-queryAll()中的MarkLogic数据移动

Next.js无法从外部本地主机获取图像

如何在下一个js中更改每个标记APEXCHARTS图表的 colored颜色

为什么在运行于<;img>;事件处理程序中的JavaScript中x和y是不可变的?

固定动态、self 调整的优先级队列

React数组查找不读取变量

Firebase函数中的FireStore WHERE子句无法执行

在使用JavaScript以HTML格式显示Google Drive中的图像时遇到问题

不允许在对象文本中注释掉的属性