简体   繁体   English

动态地将变量分配给对象属性

[英]assign variable dynamicly to object properties

I want to assign a variable to an object property. 我想将变量分配给对象属性。

this.dir = {
  origin : {
     lat :36.7014631
    , lng: -118.7559974
  },
  destination: { lat: 36.7014631, lng: -118.7559974}
}

The above code works fine, but what I need below does not work: 上面的代码工作正常,但是我下面需要的却不起作用:

this.dir = {
  origin : {
     lat :this.la,
     lng: this.lo
  },
  destination: { lat: 36.7014631, lng: -118.7559974}
}

I need this to bind in HTML like this: 我需要像这样在HTML中进行绑定:

<agm-map [latitude]="lat" [longitude]="lng">
  <agm-direction *ngIf="dir" [origin]="dir.origin" [destination]="dir.destination"></agm-direction>
</agm-map>
<agm-map [latitude]="lat" [longitude]="lng">
  <agm-direction *ngIf="dir" [origin]="dir.origin" [destination]="dir.destination"></agm-direction>
</agm-map>

You are attempting to set lat to [latitude] yet, according to your provided code, you have not defined lat as its own variable. 您正在尝试将lat设置为[latitude]但是根据您提供的代码,尚未将lat定义为其自己的变量。

You have the field: 您具有以下字段:

this.dir = {
  origin : {
     lat :this.la, 
     lng: this.lo
  },
  destination: { lat: 36.7014631, lng: -118.7559974}
}

So, change your template to not access lat , because it isn't defined in your controller, but rather access dir.origin.lat : 因此,将模板更改为不访问lat ,因为它不是在控制器中定义的,而是访问dir.origin.lat

<agm-map [latitude]="dir.origin.lat" [longitude]="dir.origin.lng">
  <agm-direction *ngIf="dir" [origin]="dir.origin" [destination]="dir.destination"></agm-direction>
</agm-map>

Or, since it looks like you have a lat & long field already do it like so: 或者,因为看起来您已经有了纬度和经度字段,所以请这样做:

<agm-map [latitude]="la" [longitude]="lo">
  <agm-direction *ngIf="dir" [origin]="dir.origin" [destination]="dir.destination"></agm-direction>
</agm-map>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM