Binding 'this' in Angular Material Autocomplete displayWith using Angular 5

前端 未结 5 749
借酒劲吻你
借酒劲吻你 2021-01-04 10:50

I was trying to use the Material Angular autocomplete and I came across the displayWith function which can be apparently used to be the output that is displayed on selection

5条回答
  •  一生所求
    2021-01-04 11:37

    You could just change your template to be

    
    

    Inside of templates this is a reference to your Component. Then just change your function to

    displayFn(id, _this) {
      return _this.getValue(id)
    }
    

    If [displayWith] needs to be a function, you could create a property that returns your displayFn like this:

    get createDisplayFn() {
      return (id) => {
        return this.getValue(id)
      }
    }
    

    and change your binding to [displayWith]="createDisplayFn". As ES6 arrow function can't be rebinded, this should still be a reference to your component.

提交回复
热议问题