Javascript 角度4-将一个新项目从一个可观察对象推到一个数组中
我有一个页面,其中显示了一个包含Javascript 角度4-将一个新项目从一个可观察对象推到一个数组中,javascript,arrays,angular,typescript,observable,Javascript,Arrays,Angular,Typescript,Observable,我有一个页面,其中显示了一个包含位置的列表,单击其中的每个位置,我将显示该位置的资产。我已经设置了如下模板: <li *ngFor="let location of locations" (click)="select(location)" droppable [dragOverClass]="'drag-target-border'" (onDrop)="onItemDrop($event, location)" > {{
位置的列表,单击其中的每个位置,我将显示该位置的资产
。我已经设置了如下模板:
<li
*ngFor="let location of locations"
(click)="select(location)"
droppable
[dragOverClass]="'drag-target-border'"
(onDrop)="onItemDrop($event, location)"
>
{{ location.name }}
<ul *ngIf="currentLocation == location && assetsVisible">
<ng-container *ngIf="currentLocation.assets?.length > 0;else message">
<li
*ngFor="let asset of currentLocation.assets"
class="asset"
draggable
[dragData]="asset"
>
<p>{{ asset.title }}</p>
<p>{{ asset.address }}</p>
<p>{{ asset.value }}</p>
</li>
</ng-container>
<ng-template #message>No assets for {{ location.name }}</ng-template>
</ul>
</li>
select(location:any = {){
if (this.currentLocation != location || !this.assetsVisible) {
this.assetsVisible = true;
}
else {
this.assetsVisible = false;
}
this.currentAddress = address;
this.locationService.getAssets(location.Id)
.subscribe(
assets => this.assets = assets
);
}
this.locationService.getAssets(location.Id).subscribe( (assets) => {
this.assets = assets;
if (asset) {
this.assets.push(asset)
}
});
然后在方法select
中,我获取的资产如下所示:
<li
*ngFor="let location of locations"
(click)="select(location)"
droppable
[dragOverClass]="'drag-target-border'"
(onDrop)="onItemDrop($event, location)"
>
{{ location.name }}
<ul *ngIf="currentLocation == location && assetsVisible">
<ng-container *ngIf="currentLocation.assets?.length > 0;else message">
<li
*ngFor="let asset of currentLocation.assets"
class="asset"
draggable
[dragData]="asset"
>
<p>{{ asset.title }}</p>
<p>{{ asset.address }}</p>
<p>{{ asset.value }}</p>
</li>
</ng-container>
<ng-template #message>No assets for {{ location.name }}</ng-template>
</ul>
</li>
select(location:any = {){
if (this.currentLocation != location || !this.assetsVisible) {
this.assetsVisible = true;
}
else {
this.assetsVisible = false;
}
this.currentAddress = address;
this.locationService.getAssets(location.Id)
.subscribe(
assets => this.assets = assets
);
}
this.locationService.getAssets(location.Id).subscribe( (assets) => {
this.assets = assets;
if (asset) {
this.assets.push(asset)
}
});
我想做的是,由于我正在使用,我想让用户能够将资产从一个位置拖到另一个位置。然后将该资源临时推送到数组assets
。
我为此做了一个函数:
onItemDrop(e: any, location:any = {}) {
this.assets = [];
this.assets.push(e.dragData);
this.select(location);
}
因此,当用户将资源从一个位置拖放到另一个位置时,我清空资源
数组,并将拖动到数组中的新资源推送到数组中。
但是,在select
方法中,我将获得资产
被拖放到的新位置
的所有资产,这将重写资产
数组。
如何将这些资产
推送到同一个数组中,以便添加新的资产
,以及从服务端点获得的位置的资产
?
我还尝试了另一种方法,首先清空数组资产,然后调用newlocation
并将newasset
传递给select
方法:
onItemDrop(e: any, location:any = {}) {
this.assets = [];
this.select(location, e.dragData);
}
然后,在选择
方法中,我将资产
参数设置为可选参数,然后在我从服务获取该位置
的所有资产
后,将其推送到资产
数组:
select(location:any = {}, asset?:any = {}) {
this.locationService.getAssets(location.Id)
.subscribe(
assets => this.assets = assets,
if (asset) {this.assets.push(asset)}
);
}
但是,这不起作用,拖放到新位置的资产没有显示在模板中。如何实现这一点?在第一种方法中,您正在覆盖subscribe
函数中的资产
数组:
this.locationService.getAssets(location.Id).subscribe(
assets => this.assets = assets
);
因此,显然,您临时保存在该数组中的资产
此时将丢失
第二种方法更有意义,但在本例中使用的subscribe
是错误的:
this.locationService.getAssets(location.Id).subscribe(
assets => this.assets = assets,
if (asset) {this.assets.push(asset)}
);
在这种情况下,if(asset)…
部分作为第二个参数传递给subscribe
调用,该调用是错误回调
。您希望这样做:
<li
*ngFor="let location of locations"
(click)="select(location)"
droppable
[dragOverClass]="'drag-target-border'"
(onDrop)="onItemDrop($event, location)"
>
{{ location.name }}
<ul *ngIf="currentLocation == location && assetsVisible">
<ng-container *ngIf="currentLocation.assets?.length > 0;else message">
<li
*ngFor="let asset of currentLocation.assets"
class="asset"
draggable
[dragData]="asset"
>
<p>{{ asset.title }}</p>
<p>{{ asset.address }}</p>
<p>{{ asset.value }}</p>
</li>
</ng-container>
<ng-template #message>No assets for {{ location.name }}</ng-template>
</ul>
</li>
select(location:any = {){
if (this.currentLocation != location || !this.assetsVisible) {
this.assetsVisible = true;
}
else {
this.assetsVisible = false;
}
this.currentAddress = address;
this.locationService.getAssets(location.Id)
.subscribe(
assets => this.assets = assets
);
}
this.locationService.getAssets(location.Id).subscribe( (assets) => {
this.assets = assets;
if (asset) {
this.assets.push(asset)
}
});
希望能有帮助
更新:
我甚至不确定这个if(asset).
部分,您实现它的方式,是否作为错误回调。如果调用回调是因为需要函数,而不是表达式(但正如我所说,我不确定这一点),则可能会出现错误。这是个错误,非常感谢您的帮助!