我在角度模型中定义了以下代码段。但是当我尝试在组件中获取数组的长度时,它表示未定义。我无法弄清楚发生了什么。请任何人帮助我。提前致谢。
import {Injectable} from "@angular/core";
@Injectable()
export class Bus{
unique_id : string = '';
bus_number : string = '';
name : string = '';
make : string = '';
model : string = '';
owner_id : string = '';
merchant_route_id : string;
features : any = [];
selected_features : any = [];
active_bus_images : any = [];
constructor(){}
}
我的服务代码段
private bus: Bus = new Bus();
private busImage: BusImage = new BusImage();
getBus(): Bus {
var bus: Bus = {
name: this.bus.name,
bus_number: this.bus.bus_number,
make: this.bus.make,
model: this.bus.model,
unique_id: this.bus.unique_id,
owner_id: this.bus.owner_id,
features: this.bus.features,
active_bus_images: this.bus.active_bus_images,
merchant_route_id : this.bus.merchant_route_id,
selected_features : this.bus.selected_features
};
console.log(this.bus.selected_features)
return bus;
}
组件中的代码段
this.bus = this.busService.getBus();
for (let item2 of this.tempFeatuers) {
this.feature2.id = item2.unique_id;
this.feature2.itemName = item2.name;
this.dropdownList.push(Object.assign({}, this.feature2));
}
console.log(this.bus.selected_features.length); // says undefined