简体   繁体   English

通过getter Typescript Angular 2更改变量值

[英]Change variable values through getter Typescript Angular 2

I'm trying to change the value in a Variable if the recieved content from a Json is empty. 如果从Json收到的内容为空,我试图更改变量中的值。 My approach is it to alter my getter Method. 我的方法是改变我的吸气剂方法。 For some reasons it seems like the getter Method doesn't get called and it changes nothing, even though I make a call on it. 由于某些原因,即使我调用了getter方法,它似乎也没有被调用并且它什么也没有改变。 Maybe you Guys can help me. 也许你们可以帮助我。

Here I try to customise my getter, but at runtime it doesn't get used: 在这里,我尝试自定义我的吸气剂,但在运行时不会被使用:

export class Chart {
   private _options: any;

   get options(): any {
     if (this._options){
        this._options = {rotation: Math.PI, circumference: Math.PI};
        return this._options;
     } else {
        console.log(this._options);
        return this._options;
     }
   }
}

And here is my component.ts class where I try to use this Variable: 这是我的component.ts类,在这里我尝试使用此变量:

import {Component, Input, OnInit} from '@angular/core';
import {Chart} from "../main-content/entity/dashboard-config-fetcher";


@Component({
  selector: 'app-chart',
  templateUrl: './chart.component.html',
  styleUrls: ['./chart.component.css']
})
export class ChartComponent implements OnInit {

  @Input()
  chart: Chart;

  public options: any;

  constructor() { }

  ngOnInit() {
    this.options = this.chart.options;
    }
}

Thanks in Advance 提前致谢

If i understood you correctly I think ur missing a ! 如果我对您的理解正确,我想您想念一个!

if (this._options) --> if(!this.options)

Otherwise you will return undefined if there is nothing stored in the _options variable 否则,如果_options变量中未存储任何内容,则将返回undefined

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

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