gpt4 book ai didi

angular - 警告 : sanitizing unsafe style value background-color

转载 作者:太空狗 更新时间:2023-10-29 16:56:23 25 4
gpt4 key购买 nike

我使用 Angular 从 Firebase 中提取数据。我希望用户的聊天消息基于用户选择的颜色 item.color。对于使用蓝色的用户,我收到了WARNING: sanitizing unsafe style value background-color:blue (see http://g.co/ng/security#xss).

我的 HTML:

<div *ngFor="let item of items; let i = index">
<ion-card style="background-color:{{item.color}}" [ngClass]="{'me': item.sender == sender, 'notme': item.sender != sender}">
<ion-card-header *ngIf="item.sender != sender">
@{{item.sender}}
</ion-card-header>
<ion-card-content>
{{item.message}}
</ion-card-content>
</ion-card>
</div>

我的TS:

import { Component, OnInit, ViewChild } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database-deprecated';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase/app';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'page-chat',
templateUrl: 'chat.html'
})
export class ChatPage{

@ViewChild(Content) content: Content;

user: {};
style;

ionViewDidLoad(){
firebase.auth().onAuthStateChanged((user)=> {
this.user = user;
console.log('authState',user);
if (user) {
var uid = user.uid;
firebase.database().ref('/userprofile/' + uid + '/' + 'chatcolor').once('value').then((snapshot)=> {
this.color = (snapshot.val());
});
}
});
}


constructor(public af: AngularFireDatabase, private Svc: Service, private sanitizer: DomSanitizer) {
this.style = sanitizer.bypassSecurityTrustStyle("blue")
}

}

我需要做什么才能做到这一点?

最佳答案

我刚刚遇到了同样的问题。我已经用这个应答器解决了这个问题(感谢 Sape The Mape):

[ngStyle]="{'background-color': item.color}"

我想深化,我发现了这篇关于 Angular 中样式的好文章:dynamic styles以及关于 binding style 的官方文档

希望对你也有帮助:)

关于angular - 警告 : sanitizing unsafe style value background-color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48435113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com