上一篇说到了Checkbox List的公共组件提取,现在说一下Radio List的公共组件提取。

Radio List组件提取起来很方便,不想Checkbox那么复杂。

radio-list.component.ts

 import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { RadioItem } from '../../model/radio';
import { NgModel } from '@angular/forms'; @Component({
selector: 'app-radio-list',
templateUrl: './radio-list.component.html',
styleUrls: ['./radio-list.component.css']
})
export class RadioListComponent implements OnInit {
@Input() list: RadioItem[];
@Input() name: string;
@Input() colNum: number = 6;
@Input("selectModel") model: string;
@Output("selectChange") onChange: EventEmitter<any> = new EventEmitter<any>(); constructor() { } ngOnInit() { }
changeSelected() {
let data = { value: this.model, name: this.name };
this.onChange.emit(data);
}
}

radio-list.component.html

 <div *ngIf="list" class="form-row">
<div class="col-{{colNum}} mb-2" *ngFor="let item of list">
<div class="form-check abc-radio abc-radio-primary">
<input class="form-check-input" type="radio" [value]="item.id" [(ngModel)]="model" (change)="changeSelected()" name="{{name}}" id="{{name}}_{{item.id}}">
<label class="form-check-label" for="{{name}}_{{item.id}}">
{{item.name}}
</label>
</div>
</div>
</div>

在相关引用的module中注册

 import { RadioListComponent } from '../components/radio-list/radio-list.component';
export const routes = [
{ path: '', component: xxxComponent, pathMatch: 'full' }
]; @NgModule({
imports: [...],
declarations: [...
, RadioListComponent
, ...],
providers: []
})
export class xxxModule {
static routes = routes;
}

对应的html中引用如下:

 <app-radio-list [list]="sourceArr"
[name]="'selectedSource'"
[colNum]="12"
[(selectModel)]="selectedSource"
(selectChange)="selectChange($event)">
</app-radio-list>

按照如上步骤,还缺少对应的selectChange($event):

 selectChange(model: any) {
this[model.name] = model.value;
}

最新文章

  1. 设计模式之命令模式(Command)
  2. ADO.NET+Access: 2,至少一个参数没有被指定值
  3. th固定 td滚动的表格实现
  4. [AngularJS] Best Practise - Resolve promises in router, defer controllers
  5. (译文)12个简单(但强大)的JavaScript技巧(一)
  6. qt容器在并发时需要注意的地方
  7. php的一些数组用法
  8. 关于HTML编辑页面(1)
  9. 谈谈UIView的几个layout方法-layoutSubviews、layoutIfNeeded、setNeedsLayout...
  10. Spring 依赖注入之从不会到稍微会一点儿
  11. Hive on ES
  12. 开启Apache的server status监测
  13. jQuery的一些基本的函数和用jQuery做一些动画操作
  14. nginx + gunicorn + flask项目发布
  15. 《http权威指南》读书笔记16
  16. DNA Consensus String
  17. make V=1 查看完整的gcc编译信息
  18. 大数据入门第十九天——推荐系统与mahout(一)入门与概述
  19. java后台读取/解析 excel表格
  20. Python2.7升级至Python3.6

热门文章

  1. linux mint 18.2 安装wireshark
  2. Django获取Header中的信息
  3. C 扩展库 - mysql API
  4. Spring声明式事务为何不回滚
  5. UVM序列篇之一:新手上路
  6. jdk1.8 HashMap源码讲解
  7. Linux du查询文件大小
  8. 基于 Annotation 的 Spring AOP 权限验证方法的实现
  9. Spring注解_详解
  10. ios开发 学习积累20161024~20161026