Angular 4在模板中随机从数组中设置一个字符串值,并在两秒后更改为另一个字符串。
创始人
2024-10-15 20:00:51
0

在Angular 4中,你可以使用以下代码来实现在模板中随机从数组中设置一个字符串值,并在两秒后更改为另一个字符串:

  1. 首先,在组件的 TypeScript 文件中定义一个字符串数组和一个用于存储当前字符串值的变量:
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {
  strings: string[] = ['String 1', 'String 2', 'String 3'];
  currentString: string;

  ngOnInit() {
    this.setCurrentString();
  }

  setCurrentString() {
    this.currentString = this.strings[Math.floor(Math.random() * this.strings.length)];
    setTimeout(() => {
      this.setCurrentString();
    }, 2000);
  }
}
  1. 在模板文件中,使用双花括号语法将当前字符串值显示出来:

{{ currentString }}

这样,每两秒钟,当前字符串值就会随机从数组中选择一个,并在两秒后更改为另一个字符串。

相关内容

热门资讯

实测分享!wepoker免费钻... 实测分享!wepoker免费钻石,约局吧游戏挂,必赢方法(有挂软件)关于wepoker免费钻石机制的...
揭秘真相!pokemmo脚本辅... 揭秘真相!pokemmo脚本辅助下载,hhpoker的辅助是真的吗,必备教程(有挂神器);1分钟了解...
重要通知!xpoker怎么作弊... 重要通知!xpoker怎么作弊,德州局怎么透视,细节方法(有挂技巧);德州局怎么透视软件透明挂更新新...
教学盘点!we poker辅助... 教学盘点!we poker辅助器v3.3,aapoker透视方法,详细教程(有挂教程)科技教程也叫必...
热点推荐!hhpkoer辅助器... 热点推荐!hhpkoer辅助器视频,wepoker破解版内购,必胜教程(有挂神器);1.hhpkoe...
三分钟了解!wejoker辅助... 1、三分钟了解!wejoker辅助脚本,wpk作弊最怕三个东西,新2025教程(有挂攻略)(UU p...
盘点十款!wpk俱乐部辅助器,... 盘点十款!wpk俱乐部辅助器,wepoker免费透视,攻略教程(有挂透视);1分钟了解详细教程(微信...
免费测试版!约局吧德州有挂吗,... 免费测试版!约局吧德州有挂吗,拱趴大菠萝辅助神器,详细教程(有挂神器);小薇(透视辅助)致您一封信;...
玩家攻略推荐!poker wo... 玩家攻略推荐!poker world辅助,hhpkoer辅助挂是真的吗,我来教教你(有挂技巧);1分...
最新研发!哈糖大菠萝怎么开挂,... 最新研发!哈糖大菠萝怎么开挂,哈糖大菠萝挂,教你攻略(有挂辅助);致您一封信;亲爱哈糖大菠萝怎么开挂...