서비스를 사용하여 내 json 데이터를 app 구성 요소에로드합니다.각도 2 : self.context.data.style이 정의되지 않았습니다.
this.data = this.dataService.getData()
.subscribe(
data => {
this.data = data;
this.ui = this.data.style;
},
err => console.error(err),
() => console.log('Data loaded')
);
NgStyle의 템플릿 구성 요소에서 this.ui에 액세스하려고하면 "Self.context.ui is undefined"오류가 발생합니다. 콘솔에 표시되기 때문에 이상하지만 일단 구성 요소에 추가하면 문제가 발생합니다.
이 코드는 응용 프로그램 구성 요소에, 나는 JSON 구조 내 JSON 여기
<h1 [NgStyle]="{'color': ui.colors.first}"> Random text </h1>
에서 역동적 인 스타일을 가지고있다 시도하고,이 같이 보입니다.
{
"style":{
"colors": {
"first": "#ffffff"
}
}
}
각도가 왜 데이터를 사용할 수 없는지, 왜 여기에서 일어나고 있다고 생각합니까?
안전한 탐색 연산자를 사용해 보셨습니까? 'Ui? .colors? .first' – echonax
'[NgStyle]'대신'[ngStyle]'이어야합니다. – Alex
그것은'ui.style.colors.first'이어야합니다. –