前情
因爲要將後端查詢回來的標題名稱顯示到頁面上,但是有得標題名稱十分的長並且是系列課程的話,顯示在頁面中就會無法區分,所以特地將名稱的開頭與結尾顯示出來,中間內容省略。
代碼
const dataUrl = 'brainstorming-web/brainStorming/queryListByStatesClassId/'
+ this.states + '/' + this.classId;
this.http.get(dataUrl).subscribe(
res => {
if (res.json().code === '0000') {
if (res.json().data.length > 0) {
// 查詢回來的內容
this.test = res.json().data;
for (let i = 0; i < this.test.length; i++) {
// 將每條數據中的名稱進行截取
let name = this.test[i].theme;
let title = '';
const nameLength = name.length;
console.log('看一下name長度', nameLength);
if (nameLength > 4) {
let titleLength = nameLength - 4;
while (titleLength > 0) {
title += '.';
titleLength--;
}
name = name.substr(0, 4) + title + name.substr(name.length - 2, name.length);
}
console.log('看一下效果-----', name);
}
}
}
}
);
顯示結果
學習古詩……列一