【问题标题】:how to pass a variable in attribute value while creating it in typescript在打字稿中创建变量时如何在属性值中传递变量
【发布时间】:2017-10-04 05:31:37
【问题描述】:
this.listvalue;
list.forEach((v: any) => {

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


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

    });
  });
});

我想在v2.value_0 =>v2.this.listvalue 的位置传递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);
    }
  }
}

【问题讨论】:

  • 你能添加完整的代码文件吗.. 看来你可能没有正确声明listvalue
  • ` @Input() listvalue:any;` 这是我认为它声明的代码的顶部
  • ` private createSearchList(list: any) { this.searchlist = [];这个.listvalue; list.forEach((v: any) => { v.data.forEach((v2: any) => { this.searchlist.push({ header:v.header, value:v2.value_0 }); }); }); } `
  • 删除后没有任何反应
  • 你有什么请用代码更新你的问题..

标签: javascript arrays angular object typescript


【解决方案1】:

你说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
            }); 
        }); 
    }); 
 }

【讨论】:

  • 你误会了......我不想在 V2.value_0 中分配 listvalue 。实际上,我想在 v2 中调用该属性,该属性位于 listvalue 中,例如 v2.this.listvalue
  • @HASSANMEHMOOD 你应该可以使用v2.listvalue 否做到这一点?
  • 但列表值超出了函数范围,我必须通过 this.listvalue 调用它
  • value: v2[this.listvalue] 是否为您工作?或者,您可以将正确的 listvalue 作为参数传递给此函数以将其纳入范围。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 2019-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多