【发布时间】:2018-01-09 20:00:33
【问题描述】:
我正在 angular2 上执行一个循环,其中包含使用 org.apache.commons:commons-lang3 库在 android 中编码的动态表情符号值,例如 \uD83D\uDEB5\uD83D\uDEB5\uD83D\uDEB5。我需要在 angular2 前端解码它们。在 itemsArr[index]['Posted Content'] = item[0]['document']['post_content'];我从后端获取编码内容。和
这就是我试图向他们展示的方式
代码如下
ngOnInit() {
this.loading = true;
var itemsArr = [];
var reporterPromiseArr = [];
var postPromiseArr = [];
var posts = this._cb.getRelationalDataFromTable('reportPost').then(res => {
res.forEach(item => {
itemsArr.push({ 'Reported On': item.document.createdAt, 'Posted By': '', 'Posted On': '', 'postId': item.document.post_id });
postPromiseArr.push(this._cb.getRelationalDataFromTableNew('userActivityStream', [{ key: '_id', value: item.document.post_id }], ['product_id', 'user_id']));
reporterPromiseArr.push(this._cb.getRelationalDataFromTable('registration', [{ key: '_id', value: item.document.user_id }]));
});
return Promise.all(postPromiseArr);
}).then(res => {
res.forEach((item, index) => {
itemsArr[index]['Posted By'] = (item[0]['document']['user_id'] !== null) ? item[0]['document']['user_id']['document']['user_name'] : '';
itemsArr[index]['Posted On'] = item[0]['document']['createdAt'];
itemsArr[index]['Posted Type'] = item[0]['document']['type'];
itemsArr[index]['Total Likes'] = item[0]['document']['total_like'];
itemsArr[index]['Posted Content'] = item[0]['document']['post_content'];
itemsArr[index]['Image'] = decodeURI(item[0]['document']['image']);
});
//console.log(reporterPromiseArr);
return Promise.all(reporterPromiseArr);
}).then(res => {
res.forEach((item, index) => {
itemsArr[index]['Reported By'] = item[0]['document']['user_name'];
});
this.listingArr = itemsArr;
this.loading = false;
}).catch(err => {
this.loading = false;
console.log(err)
});
console.log(itemsArr);
//return Promise.all(reporterPromiseArr);
}
html as follows where i tried to show them in innerHTML but not working
<form class="tr" #requestData="ngForm" (ngSubmit)="onSubmit(requestData)" *ngFor="let item of listingArr; let i = index">
<div class="td">{{i + 1}}</div>
<div class="td">{{ item['Reported By'] }}</div>
<div class="td">{{item['Reported On'] | date: yMMMdjms }}</div>
<div class="td">{{ item['Posted By'] }}</div>
<div class="td">{{ item['Posted On'] | date: yMMMdjms }}</div>
<div class="td">{{ (item['Total Likes'] == null)?0:item['Total Likes'] }}</div>
<div class="td">{{ item['Posted Type'] }}</div>
<div [innerHTML]="item['Posted Content']"></div>
</div>
</form>
【问题讨论】:
-
你能提供你的代码吗?
-
也许问题不在于 decodeURIComponent,而在于其他问题......如果没有看到实际代码,很难说。
-
感谢您的快速回复。原始代码在 angular2 中。我用它编辑了我的帖子。正如您在 itemsArr[index]['Posted Content'] = this.doSomething(item[0]['document']['post_content']); 上看到的行我调用 dosomething 函数
-
decodeURIComponent('\uD83D\uDEB5\uD83D\uDEB5\uD83D\uDEB5') === '\uD83D\uDEB5\uD83D\uDEB5\uD83D\uDEB5'—decodeURIComponent似乎与此无关。 -
嗯,您可以在控制台中输入
'\uD83D\uDEB5\uD83D\uDEB5\uD83D\uDEB5',它也会这样做......
标签: javascript angular apache-commons-lang3