Arrays Angular 4-将变量声明为对象的属性时绑定不起作用

Arrays Angular 4-将变量声明为对象的属性时绑定不起作用,arrays,angular,typescript,Arrays,Angular,Typescript,我有一个页面,其中显示了一个包含位置的列表,单击其中的每个位置,我将显示该位置的资产。我已经设置了如下模板: <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){

    if (this.currentLocation != location || !this.assetsVisible) {
      this.assetsVisible = true;
    }
    else {
      this.assetsVisible = false;
    }

    this.currentAddress = address;

    this.locationService.getAssets(location.Id)
      .subscribe(
        assets => this.currentLocation.assets = assets
      );
  }
然后在方法
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){

    if (this.currentLocation != location || !this.assetsVisible) {
      this.assetsVisible = true;
    }
    else {
      this.assetsVisible = false;
    }

    this.currentAddress = address;

    this.locationService.getAssets(location.Id)
      .subscribe(
        assets => this.currentLocation.assets = assets
      );
  }
我想做的是,由于我正在使用,我想让用户能够将资产从一个位置拖到另一个位置。然后将该资产临时推送到属性
currentLocation.assets
的一个数组中,该资产被放到该数组中。 我为此做了一个函数:

onItemDrop(e: any, location) {
    this.select(location);
    this.currentLocation.assets.push(e.dragData);
}
这会将资源从一个位置拖放到另一个位置,但我得到一个错误:

错误类型错误:无法读取未定义的属性“push”

我已经在select和onItemDrop方法中完成了
console.log(this.currentLocation.assets)
,这两次我都得到:

未定义

但是,如果我在这两个函数中都使用
console.log(this.currentLocation)
,则会得到一个对象,该对象的属性为空的
assets
数组。我可以访问该对象的所有其他属性,但我不能访问
资产
数组。 另外,当我登录到console.log
ngOnInit
时,我会在控制台中得到一个空数组:

console.log(this.currentLocation.assets);
所以,我不确定为什么会出现这个错误,为什么会得到一个属性为空的
assets
数组的对象,但我不能直接访问该对象的属性,而我可以访问同一对象的所有其他属性

这是整个组件:

import { Component, OnInit } from '@angular/core';
import { LocationService } from '../services/location.service';


@Component({
  moduleId: module.id,
  selector: 'sd-addresses',
  templateUrl: 'location.component.html'
})
export class LocationsComponent implements OnInit {

  errorMessage: string;
  locations: any[] = [];
  currentLocation: any = {
    assets: any[] = []
  };
  assetsVisible: boolean = false;


  constructor(public locationService: LocationService) {}


  ngOnInit() {
    this.getLocations();
  }


  getLocations() {
    this.locationService.get()
      .subscribe(
        locations => this.locations = locations,
        error => this.errorMessage = <any>error
      );
  }

  select(location = {}) {

    if (this.currentLocation != location || !this.assetsVisible) {
      this.assetsVisible = true;
    }
    else {
      this.assetsVisible = false;
    }

    this.currentLocation = location;

    this.locationService.getAssets(location.Id)
      .subscribe(
        assets => this.currentLocation.assets = assets,
      );
  }

  onItemDrop(e: any, location:any = {}) {
    this.currentLocation.assets = [];
    this.currentLocation.assets.push(e.dragData);
    this.select(location);
  }
}
从'@angular/core'导入{Component,OnInit};
从“../services/location.service”导入{LocationService};
@组成部分({
moduleId:module.id,
选择器:“sd地址”,
templateUrl:'location.component.html'
})
导出类位置组件实现OnInit{
错误消息:字符串;
地点:任何[]=[];
当前位置:任意={
资产:任何[]=[]
};
assetsVisible:布尔值=false;
构造函数(公共位置服务:位置服务){}
恩戈尼尼特(){
这个.getLocations();
}
getLocations(){
this.locationService.get()
.订阅(
位置=>this.locations=位置,
error=>this.errorMessage=错误
);
}
选择(位置={}){
if(this.currentLocation!=位置| |!this.assetsVisible){
this.assetsVisible=true;
}
否则{
this.assetsVisible=false;
}
this.currentLocation=位置;
this.locationService.getAssets(location.Id)
.订阅(
资产=>this.currentLocation.assets=资产,
);
}
onItemDrop(e:any,location:any={}){
this.currentLocation.assets=[];
这个.currentLocation.assets.push(e.dragData);
选择(位置);
}
}

在您的声明中尝试以下操作:

  locations: any[] = [];
  currentLocation: any = {
     assets: [] 
  };

这样,您将在声明中声明
currentLocation
,它是一个对象类型
{}
,并且具有一个属性
assets
,它是一个数组类型
[]

  locations: any[] = [];
  currentLocation: any = {
     assets: [] 
  };

通过这种方式,您将声明
currentLocation
,它是一种对象类型
{}
,并且具有一个属性
assets
,它是一种数组类型
[]

是否已定义?如果是,是否定义了
此.currentLocation
?制作几个console.log并让我知道。如果未定义
,您可能必须执行
绑定(此)
。执行
控制台.log(此.currentAddress.documents)时,我会得到
未定义的
此。选择(位置)工作是否定义了此代码?如果是,是否定义了
此.currentLocation
?制作几个console.log并让我知道。如果未定义
,您可能必须执行
绑定(此)
。执行
控制台.log(此.currentAddress.documents)时,我会得到
未定义的
此。选择(位置)工作你能给我们看一下整个组件代码吗?我已经将整个组件添加到了问题中,似乎你更改了一些东西(
this.assets
而不是
currentLocation.assets
)对不起,是的,我同时更改了代码,我刚刚声明它上的数组是自己的,不是作为
currentLocation
对象的属性,当我将其声明为对象的属性时,这是可行的,但它仍然不起作用。我已经修复了代码,并将其恢复到问题中的原始状态。这将引发编译器错误<代码>当前位置:any={assets:any[]=[]}
语法无效。它应该是
currentLocation:any={assets:[]}
你能给我们看一下整个组件的代码吗?我已经把整个组件添加到了问题中似乎你改变了一些东西(
this.assets
而不是
currentLocation.assets
)对不起,是的,我同时也改变了代码,我刚刚声明它上面的数组是自己的,不是作为
currentLocation
对象的属性,当我将其声明为对象的属性时,这是可行的,但它仍然不起作用。我已经修复了代码,并将其恢复到问题中的原始状态。这将引发编译器错误<代码>当前位置:any={assets:any[]=[]}
语法无效。它应该是
currentLocation:any={assets:[]}