Angular2:生成复选框问题

时间:2016-04-14 08:50:56

标签: javascript angular

我有以下模板:

<div *ngFor="#weekday of weekdays" class="checkbox">
    <label>
        <input
            #{{weekday}}
            (change)="selectWeekday('{{weekday}}', {{weekday}}.checked)"
            type="checkbox"> {{weekday}}
    </label>
</div>

由此组件支持:

@Component({
    selector: "schedule",
    templateUrl: "app/schedule.html",
})
export class ScheduleComponent {
    public weekdays: string[] = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ];

    public selectWeekday(weekday: string, checked: boolean {
        // ...
    }
}

我收到以下错误:

  

得到插值({{}}),其中表达式位于[selectWeekday('{{weekday}}',{{weekday}},第15列。检查)]

我做错了什么?

2 个答案:

答案 0 :(得分:2)

模板变量需要静态名称。您可以使用绑定来定义这样的名称。

这是无效的:

#{{weekday}}

它需要像

#weekday

(虽然不会绑定到您的weekday属性 或

#monday

答案 1 :(得分:2)

尝试这种方法:

<div *ngFor="#weekday of weekdays" class="checkbox">
<label>
    <input
        (change)="selectWeekday(weekday, $event.target.checked)"
        type="checkbox"> {{weekday}}
</label>
</div>