Angular中的select

#angular中的select
我们经常会遇到通过select来进行选择的情况,需要向select的option中传递值,也需要从当前选中的option中获取值,本文将简介这一过程。

<div class="row">
    <div class="col-sm-2">
        <form class="form-horizontal" role="form">
            <div class="form-group" style="margin:auto;height:39px;">
                <label for="name" class="col-sm-2 control-label">type: </label>
                <div class="col-sm-10">
                    <select class="form-control" name="reportType" [ngModel]="reportType" (ngModelChange)="generateReportNameList($event)">
                        <option name="reportType" value="weekly"  ngDefaultControl>weekly</option>
                        <option name="reportType" value="monthly" ngDefaultControl>monthly</option>
                        <option name="reportType" value="quarterly" ngDefaultControl>quarterly</option>
                        <option name="reportType" value="semi-annual" ngDefaultControl>semi-annual</option>
                        <option name="reportType" value="annual" ngDefaultControl>annual</option>
                    </select>
                </div>
            </div>
        </form>
    </div>
    <div class="col-sm-4">
        <form class="form-horizontal" role="form">
            <div class="form-group"  style="margin:auto;height:39px;">
                <label for="name" class="col-sm-2 control-label">time: </label>
                <div class="col-sm-10">
                <select class="form-control">
                    <option *ngFor="let reportName of reportNameList" [innerHTML]="reportName"></option>
                </select>
            </div>
            </div>
        </form>
    </div>
</div>

上面的html代码实现了两个并排的select,选择左边的select可以选择不同的report类型,同时右边select中的option显示相对应的report name列表,以便选择某类型的某个report。
准备工作:有一个reportName 的list,有一个根据不同类型来map reportName list的函数generateReportNameList(type:string)。
关键点在于,如何监听选中的option的变化,在angular中,有一套专门的机制来适应select。
[ngModel]="reportType"可以理解为将reportType这个变量的值赋给ngModel,(ngModelChange)="generateReportNameList($event)"则实现了对generateReportNameList函数的调用,ngModelChange的作用可以理解为每次change选中的option时执行相对应的动作(函数)。
左边的select做的一系列工作将reportName list更新为了相对应其type的,于是右边的select就可以愉快的展示以及选择report name list啦~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值