我创建了一个聊天界面,每当我向对话流发送消息或从对话流接收消息时,它们都会出现在屏幕的左侧。我希望我的消息在右边&对话流消息在左边&两个聊天气泡应该是不同的颜色。我不知道如何区分它们。有人能帮我吗?这是我的HTML代码。
<ion-content>
<div style="font-size:10px; text-align:center;">7 minutes ago</div>
<div class="msgbubble" *ngFor="let msg of messages">
<div class="innermsg">
{{ msg }}
</div>
</div>
</ion-content>
<ion-footer>
<ion-toolbar>
<ion-input placeholder="Type here" [(ngModel)]="question" type="text"></ion-input>
<ion-buttons right>
<button ion-button clear icon-only id="sendicon" (click)="ask(question)">
<ion-icon name="send"></ion-icon>
</button>
</ion-buttons>
</ion-toolbar>
</ion-footer>这是ts代码。
ask(question) {
try {
this.messages.push(question);
var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/json' );
headers.append('Authorization', 'Bearer ' + this.Access_token)
let options = new RequestOptions({ headers: headers });
let postParams = {
"lang": "en",
"query": question ,
"sessionId": "12345",
"timezone": "America/New_York"
}
try{
this.http.post("https://api.dialogflow.com/v1/query?v=20150910", postParams, options)
.subscribe(data => {
let obj = JSON.parse(data['_body']);
this.answer = obj.result.fulfillment.speech;
//console.log(data['_body']);
console.log(this.answer);
this.messages.push(this.answer);
}, error => {
console.log(error);// Error getting the data
});
}
catch(e){
console.log(e);
}
}
catch(e){
console.log(e);
}
}我希望用户界面应该是作为一个一般的聊天页面/机器人。在右边发送消息,在左边接收消息。
发布于 2019-01-17 22:23:32
数组" message“中的对象需要有一个属性来指示消息是question还是answer (如果您将”message“接口添加到您的问题中会更好)。在angular中,您可以按如下条件更改css-classes:
<div [className]="condition ? 'example-class' : 'other-class'"></div>希望这能解决你的问题!
https://stackoverflow.com/questions/50762434
复制相似问题