[英]Angular 2: self.context.data.style is undefined
I am loading using a service to load my json data on the app component.我正在使用服务加载应用程序组件上的 json 数据。
this.data = this.dataService.getData()
.subscribe(
data => {
this.data = data;
this.ui = this.data.style;
},
err => console.error(err),
() => console.log('Data loaded')
);
When I try to access this.ui in the template component for NgStyle, I get the error "Self.context.ui is undefined".当我尝试在 NgStyle 的模板组件中访问 this.ui 时,出现错误“Self.context.ui 未定义”。 Its strange because it shows in the console, but once I add it on my component, it freaks out.
这很奇怪,因为它显示在控制台中,但是一旦我将它添加到我的组件中,它就会吓坏了。
This code is in the app component, I am trying to have dynamic styles from my json此代码在应用程序组件中,我正在尝试从我的 json 中获取动态样式
<h1 [NgStyle]="{'color': ui.colors.first}"> Random text </h1>
Here is the json structure, it looks something like this.这是json结构,它看起来像这样。
{
"style":{
"colors": {
"first": "#ffffff"
}
}
}
I don't really know why angular won't let me use the data, what do you think is happening here?我真的不知道为什么 angular 不让我使用数据,你认为这里发生了什么?
Your self.context
error is here:您的
self.context
错误在这里:
this.data = this.dataService.getData()
should be just:应该只是:
this.dataService.getData()
.subscribe(
data => {
this.data = data;
this.ui = this.data.style;
},
err => console.error(err),
() => console.log('Data loaded')
);
You are assigning data to this.data
inside this.data
so that's why it's complaining about self.context
.您正在将数据分配给
this.data
内的this.data
所以这就是它抱怨self.context
的原因。
and [NgStyle]
should be [ngStyle]
and like @echonax suggested you might have to use safe operators.并且
[NgStyle]
应该是[ngStyle]
并且像 @echonax 建议您可能必须使用安全运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.