【问题标题】:ngSemantic select doesn't bind to objectngSemantic select 不绑定到对象
【发布时间】:2016-09-26 08:01:40
【问题描述】:

我正在使用 Angular2 和 ngSemantic 库。我创建了一个表单,这是我的代码:

模板文件:

<div class="container">
    <h1>My first form</h1>
    <form class="ui form" [formGroup]="myForm">
        <sm-loader [complete]="!submitted" class="inverted" text="Loading..."></sm-loader>

        <div class="field">
            <sm-select [(model)]="firstSelect" [control]="myForm.controls.category" placeholder="Search..." class="fluid search">
                <option *ngFor="let item of itemslist" [value]="item">{{item.name}}</option>
            </sm-select>
        </div>

        <div class="field">
            <sm-select placeholder="Search..." class="fluid search">
                <option *ngFor="let i of firstSelect?.items">{{i}}</option>
            </sm-select>
        </div>

        <sm-button (click)="onSubmit()" [disabled]="!myForm.valid" class="primary">Login</sm-button>
    </form>
</div>

组件文件:

itemslist: any = [
    { name: 'first', items: ['one', 'two', 'three']},
    { name: 'second', items: ['one', 'two', 'three']},
    { name: 'third', items: ['one', 'two', 'three']}
];

constructor(private dataService: DataService, fb: FormBuilder) {
    this.myForm = fb.group({
        'category': [],
        'subcategory': [],
        'name': [],
        'email': []
    });
}

第二个选择中的循环不起作用,因为firstSelect 结果是字符串而不是Object。可能发生这种情况是因为 select/option 只处理 stringnumber

我该如何解决这个问题?

【问题讨论】:

    标签: forms select angular option semantic-ui


    【解决方案1】:

    {{...}} 进行字符串插值。如果要传递对象实例,请不要使用它。

    改为使用对象绑定

    [value]="item"
    

    【讨论】:

    • 是的,我知道。我编辑了我的问题,这是一个错字。抱歉,感谢您的回复。无论如何它不起作用。
    • 我不知道sm-select,但对于普通选择,您需要在绑定对象而不是&lt;options&gt; 元素上的字符串时使用[ngValue]="..."
    • 我知道。如果在option 上使用selectngValue 它可以工作:(
    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2012-05-31
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2011-04-24
    相关资源
    最近更新 更多