在Angular 5中,可以使用concat
方法来合并两个数组。以下是一个示例代码:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
Merged Array:
- {{ item }}
`
})
export class AppComponent {
array1: string[] = ['a', 'b', 'c'];
array2: string[] = ['d', 'e', 'f'];
mergedArray: string[] = [];
constructor() {
this.mergeArrays();
}
mergeArrays() {
this.mergedArray = this.array1.concat(this.array2);
}
}
在上述代码中,我们有两个字符串数组array1
和array2
,我们使用concat
方法将它们合并为一个新的数组mergedArray
。在构造函数中,我们调用mergeArrays
方法来执行合并操作。最后,我们在模板中使用*ngFor
指令来循环遍历mergedArray
并显示每个元素。
通过以上代码,我们可以得到以下输出:
Merged Array:
a
b
c
d
e
f
希望这个例子能够帮助你解决问题!