Angular5中提取公共组件之radio list的实例代码


Posted in Javascript onJuly 10, 2018

本文给大家说一下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;
 }

总结

以上所述是小编给大家介绍的Angular5中提取公共组件之radio list的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
使用jQuery实现input数值增量和减量的方法
Jan 24 Javascript
JSON字符串转JSON对象
Jul 31 Javascript
jQuery中常用的遍历函数用法实例总结
Sep 01 Javascript
Google 地图获取API Key详细教程
Aug 06 Javascript
jQuery grep()方法详解及实例代码
Oct 30 Javascript
bootstrap使用validate实现简单校验功能
Dec 02 Javascript
详解vue-cli快速构建项目以及引入bootstrap、jq
May 26 Javascript
微信小程序之网络请求简单封装实例详解
Jun 28 Javascript
浅谈js中的this问题
Aug 31 Javascript
jQuery中extend函数简单用法示例
Oct 11 jQuery
node事件循环和process模块实例分析
Feb 14 Javascript
Vue如何基于vue-i18n实现多国语言兼容
Jul 17 Javascript
ng-alain表单使用方式详解
Jul 10 #Javascript
JavaScript基于对象方法实现数组去重及排序操作示例
Jul 10 #Javascript
React之PureComponent的使用作用
Jul 10 #Javascript
详解在React.js中使用PureComponent的重要性和使用方式
Jul 10 #Javascript
echarts整合多个类似option的方法实例
Jul 10 #Javascript
详解使用Next.js构建服务端渲染应用
Jul 10 #Javascript
node.js中TCP Socket多进程间的消息推送示例详解
Jul 10 #Javascript
You might like
php时间戳格式化显示友好的时间函数分享
2014/10/21 PHP
简单谈谈PHP中的Reload操作
2016/12/12 PHP
php中的钩子理解及应用实例分析
2019/08/30 PHP
select标记美化--JS式插件、后期加载
2013/04/01 Javascript
JavaScript字符串对象的concat方法实例(用于连接两个或多个字符串)
2014/10/16 Javascript
JavaScript生成随机字符串的方法
2015/03/19 Javascript
Nodejs抓取html页面内容(推荐)
2016/08/11 NodeJs
JS无缝滚动效果实现方法分析
2016/12/21 Javascript
基于bootstrap按钮式下拉菜单组件的搜索建议插件
2017/03/25 Javascript
浅谈webpack下的AOP式无侵入注入
2017/11/12 Javascript
JS获取浏览器地址栏的多个参数值的任意值实例代码
2018/07/24 Javascript
Vuejs 实现简易 todoList 功能 与 组件实例代码
2018/09/10 Javascript
JS+CSS实现随机点名(实例代码)
2019/11/04 Javascript
VUE Elemen-ui之穿梭框使用方法详解
2021/01/19 Javascript
python中urllib模块用法实例详解
2014/11/19 Python
Python实现的摇骰子猜大小功能小游戏示例
2017/12/18 Python
django自带的server 让外网主机访问方法
2018/05/14 Python
对python的文件内注释 help注释方法
2018/05/23 Python
matplotlib subplots 设置总图的标题方法
2018/05/25 Python
bluepy 一款python封装的BLE利器简单介绍
2019/06/25 Python
Python分析彩票记录并预测中奖号码过程详解
2019/07/09 Python
python自动循环定时开关机(非重启)测试
2019/08/26 Python
Python标准库shutil模块使用方法解析
2020/03/10 Python
在Keras中CNN联合LSTM进行分类实例
2020/06/29 Python
CSS3文本换行word-wrap解决英文文本超过固定宽度不换行
2013/10/10 HTML / CSS
HTML5 预加载让页面得以快速呈现
2013/08/13 HTML / CSS
如何用H5实现一个触屏版的轮播器的实例
2017/01/09 HTML / CSS
美国最顶级的精品店之一:Hampden Clothing
2016/12/22 全球购物
大码女装:Ulla Popken
2019/08/06 全球购物
搞笑获奖感言
2014/01/30 职场文书
院领导写的就业推荐信
2014/03/09 职场文书
小学社团活动总结
2014/06/27 职场文书
五一劳动节活动总结
2015/02/09 职场文书
写给孩子的新学期寄语
2015/02/27 职场文书
七夕情人节问候语
2015/11/11 职场文书
pd.DataFrame中的几种索引变换的实现
2022/06/16 Python