angular - 当变量更改时,h2 插值不会更新
问题描述
当变量更改时,h2 插值不会更新我想要做的是将字母添加到 displayWord 变量 看起来好像输入了单词 TS
displayedWord = ''
words = ['Innovation', 'Inspiration']
changeWord = (words) => {
const wordArray = this.words[0].split('')
const lettersToDisplay = []
for (let i = 0; i < wordArray.length; i++) {
setTimeout(function () {
const letter = wordArray[i];
lettersToDisplay.push(letter)
this.displayedWord = lettersToDisplay.join('');
console.log(this.displayedWord);
}, 1000 * i);
}
}
ngOnInit(): void {
this.changeWord(this.words);
}
HTML
<h2 class="about__title">We help you discover the power of {{displayedWord}}_
解决方案
你必须改变:
setTimeout(function() { ... } )
至:
setTimeout(() => {...})
为了this
引用组件实例。
看看这里。
指出:
没有自己的绑定到
this
.
与函数表达式相比
推荐阅读
- android-recyclerview - 通过转换转换从不同来源(存储、火库)检索到的实时数据
- firebase - 如何在 firebase 上注册用户?
- javascript - 使用 Web 服务登录 Moodle 的问题
- strongloop - 如何在loopback4中执行pdf报告生成
- node.js - 如何将数据插入到 nodejs 中的模式数组中?
- r - 测试字符串是否包含字母和字符
- ubuntu - 继续使用 libcoap
- python - 如何让字符串列的所有包含匹配项?
- python - Django 表单调整 CharField/Textarea 字段的大小
- azure-resource-manager - Azure ARM - 将 StorageAccount FileShare 挂载到 linux VM