Binding select element to object in Angular

后端 未结 14 1530
走了就别回头了
走了就别回头了 2020-11-22 02:41

I\'d like to bind a select element to a list of objects -- which is easy enough:

@Component({
   selector: \'myApp\',
   template: `

My Application&

14条回答
  •  后悔当初
    2020-11-22 03:39

    In app.component.html:

     
    

    And app.component.ts:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      levelNum:number;
      levels:Array = [
          {num: 0, name: "AA"},
          {num: 1, name: "BB"}
      ];
    
      toNumber(){
        this.levelNum = +this.levelNum;
        console.log(this.levelNum);
      }
    
      selectedLevel = this.levels[0];
    
      selectedLevelCustomCompare = {num: 1, name: "BB"}
    
      compareFn(a, b) {
        console.log(a, b, a && b && a.num == b.num);
        return a && b && a.num == b.num;
      }
    }
    
        

    提交回复
    热议问题