【问题标题】:Angular 2 : Select on change to return the itemAngular 2:选择更改以返回项目
【发布时间】:2017-11-19 14:23:57
【问题描述】:

如何在更改时传递项目?

我走了这么远:

<select #sel (change)="select.emit(sel.value.url)">
        <option  *ngFor="let item of selectlist">
            {{item.description}}
        </option>
    </select>

但我想在更改时将“项目”传回。

我应该只取回项目对象

{ 值:0, 描述:“家”, 网址:'http://www.color.com' }

但我得到的是 'Home'。

这是我的完整数组

public  pagelist:Array<Object> = [
      {
        value: 0, 
        description: 'Home',
            url: 'http://www.color.com'
      },
      {
        value: 1, 
        description: 'Tours',
        subpage: [{
                    value: 0, 
                    description: 'Italy'
                 },
                 {
                    value: 0, 
                    description: 'France'
                 },
                 {
                    value: 0, 
                    description: 'London'
                 }]
      },
      {
        value: 1, 
        description: 'About us',
            url: 'http://www.color.com'
      },
            {
        value: 1, 
        description: 'Contact us',
            url: 'http://www.color.com'
      }

  ];

【问题讨论】:

    标签: angular typescript select drop-down-menu


    【解决方案1】:

    将您的代码替换为:

    <select #sel (change)="select.emit(selectlist[$event.target.value])">
        <option [value]='i' *ngFor="let item of selectlist; let i = index;">
            {{item.description}}
        </option>
    </select>
    

    【讨论】:

    • 获得了我的价值,但我想要 Item obj。
    • @ChrisTarasovs,请立即查看
    • 你有这个吗?
    • aaa 确实有效。你也可以写得更短 (change)="select.emit(selectlist[sel.value]
    • 是的,您还添加了本地变量,那么请您支持我的答案吗?
    猜你喜欢
    • 2018-02-24
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 2018-05-05
    • 1970-01-01
    • 2017-10-28
    • 2018-05-13
    相关资源
    最近更新 更多