Angular 怎样才能使{a.b}工作在有角度的环境中
我是打字脚本和angularjs的新手 1.文件中的app.component.tsAngular 怎样才能使{a.b}工作在有角度的环境中,angular,Angular,我是打字脚本和angularjs的新手 1.文件中的app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements Oninit {
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements Oninit {
title = 'Tour of Heroes';
a = {"hello":"world", "nice": "day"};
b = "hello";
c = "nice";
d : string;
ngOnInit(): void{
this.d = this.b; //or this.d = this.c;
}
}
2.in-app.component.html
{{a.b}} // actually i want it print "world" or "day"
我怎样才能使{{a.b}印刷“世界”
回答是“毗瑟奴的皮莱”和“穆罕默德·阿尔巴马维”:
对象['propertyName']可以使用变量
所以{a[b]}就是答案在javascript中,您可以通过
object.propertyName
或类似于对象['propertyName']的括号符号来访问任何对象属性
在app.component.html中,只需这样编写{{a[b]}
或{{a.hello}
阅读更多关于
如果要使用变量在html中指向对象键,请使用括号表示法
因此,在发布此类问题之前,您应该使用{a[b]}而不是{a.b}考虑阅读th文档(并学习Javascript)
{a.hello}
。最好将a的声明更改为a={hello:“world”}。到目前为止,您做了什么来解决这个问题?我知道{a.hello}是有效的。但b是一个变量,不是静态的
a = {"hello":"world"};
b = "hello";
{{a.b}} // undefined
{{a[b]}} //world