this.listvalue;
list.forEach((v: any) => {

  v.data.forEach((v2: any) => {


    this.searchlist.push({
      header: v.header,
      value: v2.value_0

    });
  });
});


我想在this.listvalue的地方传递v2.value_0 =>v2.this.listvalue,但这无法正常工作,给我也尝试过v2.valueof(this.listvalue)的语法错误,但它也无法正常工作,因为我想通过this.listvalue更改呼叫v2


  我怎样才能做到这一点?此方案的正确语法是什么?
  整个代码在这里


export class Md2Component {
  @Input() list: any;
  @Input() listvalue: any;

  public searchlist;
  public placeholder: String = "Searct the JSON";
  public switch: boolean = true;
  public switch5: boolean = true;
  public searchlistresult: any
  public data: string;
  public header1: String;
  public resultant_list: any;
  public lastone: user[] = [];
  @Output() _onSelect = new EventEmitter<any>();
  public anyinstance: any
  public user2: user

  s = '';
  public index;

  constructor() {

  }
  ngOnInit() {
    this.data = this.listvalue;
    this.createSearchList(this.list);
    this.latest(this.searchlist);

  }
  private createSearchList(list: any) {
    this.searchlist = [];
    this.listvalue;
    list.forEach((v: any) => {
      v.data.forEach((v2: any) => {
        this.searchlist.push({
          header: v.header,
          value: v2.value_0

        });
      });
    });



  }
  search1(s) {
    this.search(s);
    if (this.switch != true) {
      this.latest(this.searchlistresult)
    }
  }

  search(s) {
    this.switch = false;

    this.searchlistresult = _.filter(this.searchlist, (o: any) => {
      if (s) {

        return _.startsWith(_.lowerCase(o.value), _.lowerCase(s));
      }
      this.switch = true;
      return true;
    });
  }
  latest(list: any) {

    const arr = list;

    const keys = [];

    for (let i of arr) {
      if (!keys.includes(i.header)) keys.push(i.header);
    }

    const result = keys
      .map(k => Object.assign({header: k}, {
        data: arr.filter(x => x.header === k)
          .map(y => Object.assign({}, {value: y.value}))
      }));
    this.anyinstance = result;



  }
  generateArray(obj) {
    return Object.keys(obj).map((key) => {return obj[key]});
  }
  onSelect(header: any, value: any) {
    {
      console.log(header);
      console.log(value);
      this.s = value;
      this.user2 = {
        header: header,
        value: value
      }
      this.lastone.push({
        'header': header,
        'value': value,
      }
      );
      this.switch = true;
      this._onSelect.emit(this.user2);
    }
  }
}

最佳答案

您已经说过this.listvalue@Input(),所以我们假设它已经用一个值初始化了。

更新您可以尝试将其作为参数传递。由于listvalue的全局范围是引起问题的原因,因此将其传递给函数将使其进入正确的范围。

// Bringing listvalue into scope, to avoid use of 'this' accessor.
private createSearchList(list: any, listvalue: any) {
    this.searchlist = [];

    list.forEach((v: any) => {
        v.data.forEach((v2: any) => {

            this.searchlist.push({
                header: v.header,
                value: listvalue
            });
        });
    });
 }

关于javascript - 如何在 typescript 中创建属性值时传递变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43809939/

10-09 15:02