代码之家  ›  专栏  ›  技术社区  ›  Tommie Jones

Angular2获取错误TypeError:无法分配给对象“\<HTMLInputElement>”的只读属性“list”

  •  0
  • Tommie Jones  · 技术社区  · 7 年前

    我正在尝试组合我们使用的html模式。html如下所示

        <input class="sidebarInputDevice" type="search"  [list]="listidentx" placeholder="Search {{label}}..." (input)="deviceSelected($event.target.value)"> {{listidentx}}
    <datalist [id]="listidentx">
      <option *ngFor="let item of items">{{item.name}}</option>
    </datalist>
    

    typescript代码如下所示

        import { Component, OnInit, EventEmitter, Input, Output } from '@angular/core';
    
    
    
    @Component({
      selector: 'app-select-entry',
      templateUrl: './select-entry.component.html',
      styleUrls: ['./select-entry.component.css']
    })
    export class SelectEntryComponent implements OnInit {
    
      constructor() { 
        //this.listidentx='working';
      } 
    
      @Input() 
        items:{}[] = [];
    
      @Input() 
        label = '';
    
      @Input()
        listident='custom';
    
        listidentx = 'bacon';
    
    
      @Output() 
        valueChange: EventEmitter<number> = new EventEmitter();
    
      ngOnInit() {
        console.log(this.items);
        console.log("-----");
        for (var i=0;i<this.items.length;i++) {
          console.log(this.items[i]);
        }
        console.log("^----");
      }
    
      valSelected(value) {
        this.valueChange.emit(value);
    
      }
    }
    

    我收到以下错误

    ERROR TypeError: Cannot assign to read only property 'list' of object '#<HTMLInputElement>'
    

    是否有解决方案,或者我是否可以不将列表属性设置为动态。

    如果无法使属性动态化,可以确保每个组件的列表都有不同的id。

    1 回复  |  直到 7 年前
        1
  •  2
  •   hayden    7 年前

    要绑定到元素属性,请执行以下操作:

    <input [attr.list]="listindent">
    

    如果 listident='custom' ,在运行时,它将如下所示:

    <input list="custom">