Angular系列 -> 在 input、checkbox、radio、textarea 、select实现双向数据绑定
双向数据绑定是Angular 项目中一大特点,它属于MVVM模式,即Model 改变,影响视图,视图内容改变,反过来影响Model;其底层实现机制,就是将属性绑定和事件绑定绑定相结合。下面介绍双向数据绑定在input、checkbox、radio、textarea 标签的实现方式。
input:
<!-- input -->
<li>姓名: <input class="form_input" type="text" [(ngModel)]="peopleInfo.name" /></li>
radio:
<!-- radio --> <li>性别: <input type="radio" value="male" name="male" id="male" [(ngModel)]="peopleInfo.gender" /><label for="male">男</label> <input type="radio" value="female" name="female" id="female" [(ngModel)]="peopleInfo.gender" /><label for="female">女</label> </li>
checkbox:
<!-- checkbox --> <li>爱好: <span *ngFor="let item of peopleInfo.hobby; let key=index"> <input type="checkbox" [(ngModel)]="item.checked" [id]="'check_' + key" /> <label [for]="'check_' + key">{{item.title}}</label> </span> </li>
textarea :
<!-- textarea --> <li>备注: <br> <textarea name="comments" id="comments" cols="30" rows="3" [(ngModel)]="peopleInfo.comments"></textarea> </li>
select:
<!-- select --> <li>城市: <select [(ngModel)]="peopleInfo.city" autocomplete="off"> <!-- 设置默认选项 --> <option disabled hidden selected value="">Select</option> <option *ngFor="let city of cities" [value]="city">{{city}}</option> </select> </li>
特此声明: 以上内容是根据大地老师的教学视频做的笔记,以及结合了之前的一些理解, 非常感谢~
本文来自博客园,作者:77工作室,转载请注明原文链接:https://www.cnblogs.com/z7luv/p/15230078.html
如果您觉得阅读本文对您有帮助,请点击一下右下方的推荐按钮,您的推荐将是我写作的最大动力!版权声明:本文为博主原创或转载文章,欢迎转载,但转载文章之后必须在文章页面明显位置注明出处,否则保留追究法律责任的权利。

浙公网安备 33010602011771号