我試圖複製一個財產角(志願者到X),因爲我想編輯的財產和離開志願者的方式。下面是從TS的代碼:如何將角度屬性複製到另一個?
volunteership;
x ;
constructor(private viewvolunteershipService: Volunteership,
private router: Router) {}
ngOnInit() {
this.viewvolunteershipService.getVolunteership().subscribe(volunteership =>
this.volunteership = volunteership);
console.log("Volunteership", this.volunteership);
this.x = this.volunteership;
}
在這裏,在HTML我想打電話給在ngFor的屬性x,所以我可以從中選擇一個城市,但它顯示了我什麼。如果我使用志願者而不是x,那麼它的工作是完美的。如何將志願者複製到x,以便我可以從中選擇一個城市?
<div class="form-group" >
<label for="city" >City</label>
<select id="city" class="form-group" >
<option value=""></option>
<option *ngFor=" let z of x" >{{z.city}}</option>
</select>
</div>
我已經試圖複製一個數組
for (var i = 0, len = this.volunteership.length; i < len; i++) {
this.x[i] = this.volunteerhip[i];
}
我甚至一直在使用Object.assign()方法,仍然沒有嘗試過。
'subscribe'是異步的,這意味着'subscribe'之外的代碼在您調用服務時不會停止執行。如果你想使用你設定的'志願者'的數據,你需要在訂閱內部完成,或者保證你的服務已經取得了數據(例如在下拉菜單中調用'change'事件顯示使用'* ngIf =「志願者」') – snaplemouton