import { Component, EventEmitter, Output } from '@angular/core'; import { NavParams } from 'ionic-angular' import { PromptOption } from '../type' import { Validate } from 'validate'; const defaultOption: PromptOption = { title: '温馨提示', content: '', icon: false, okText:'确认', cancelText:'取消' } @Component({ selector: 'modal-prompt', template:'' + '' + '' + '' + '
' }) export class ModalPromptComponent { option: PromptOption = defaultOption; _value // 表单数据 input error sForm validateFormat constructor(public params: NavParams, public validate:Validate) { this.option = Object.assign({},this.option,params.data); this.input = this.option.input || {} this.validateFormat = this.input.format ? [this.input.name, this.input.format] : this.input.name; } // 获取焦点 @Output() focusEvent = new EventEmitter(); onFocus = (_value) => { this.focusEvent.emit(_value); } // 失去焦点 @Output() blurEvent = new EventEmitter(); onBlur = (_value) => { if(this.validateFormat){ const res = this.validate.checkOne(_value, this.validateFormat); if(res.passed){ this.blurEvent.emit(_value); }else{ this.error = res.errors[0].message; } }else{ this.blurEvent.emit(_value); } } // 输入变化事件 @Output() changeEvent = new EventEmitter(); onChange = (_value) => { this.changeEvent.emit(_value); } // 点击确定 @Output() okEvent = new EventEmitter(); ok = (_value) => { if(this.validateFormat){ const res = this.validate.checkOne(_value, this.validateFormat); if(res.passed){ this.okEvent.emit(_value); }else{ this.error = res.errors[0].message; } }else{ this.okEvent.emit(_value); } } // 点击取消 @Output() cancelEvent = new EventEmitter(); cancel = () => { this.cancelEvent.emit(false); } }