こんにちは。Angular2とOnsenUI2を使って開発をしているものです。
ons-dialogを使って時間の選択をさせたいと思っています。
component内にあるformのデータを使ってons-dialogの呼び出し元に値を渡したいのですが、ベストプラクティスがわかっていません。
また、現在Injectionを使って受け渡ししようと思っているのですが、このようなエラーが出ています。
Uncaught Error: Invalid providers for "MyDialogComponent" - only instances of Provider and Type are allowed, got: [?undefined?]
コードがこちらになります。
@Component({ providers: [UsersComponent], template: ` <ons-dialog animation="default" cancelable #dialog> <div class="dialog-mask"></div> <div class="dialog"> <div class="dialog-container" style="height: 330px;"> <ons-toolbar> <div class="center">施術時間の変更</div> </ons-toolbar> <div class="content"> <form id="timeForm" #timeForm="ngForm" (ngSubmit)="sendRequestTime(timeForm.value)"> <ons-list> <ons-list-header></ons-list-header> <ons-list-header></ons-list-header> <ons-list-header>開始時間</ons-list-header> <ons-list-item tappable> <input type="datetime-local" name="startingTime" required tappable class="dateInput" ngModel> </ons-list-item> </ons-list> </form> </div> <button (click)="dialog.hide()">Close</button> <button type="submit" form="timeForm">hoge</button> </div> </div> </ons-dialog> ` }) @Injectable() export class MyDialogComponent { message = ''; hage: any; constructor(params: Params, private _af: AngularFire, private _usersComponent: UsersComponent) { this.message = <string>params.at('message'); this.hage = this._usersComponent.say(); console.log(this.hage); } sendRequestTime(timeForm: any){ console.log(timeForm); //ここでデータを送りたい } }
Injectionされる側はシンプルです。
say(): string { return "foo"; }
そもそもDIを使うのがベストプラクティスなのかもわかっていないので、そちらも含めてどうするか教えていただけたらとっても嬉しいです。
@Component({ selector: 'ons-page', template: ` //テンプレートが入ります。 `, styles: [require('./css/template.css')] }) export class UsersComponent { private _dialog: any; private _destroyDialog: Function; defaultDateTime: Date; defaultPlusDateTime: Date; requestId: any; requests: FirebaseListObservable<any[]>; therapistId: any; therapistObject: any; therapistGender: string; hoge: any; requestContentArray: any[]; jage: Array<any[]>; userImageUrl: string genderSubject: Subject<any>; startingTime: any; endTime: any; constructor( private _navigator: OnsNavigator, private _params: Params, private _af: AngularFire, private _us: UserService, private _rs: RequestService, private _ds: DomSanitizer, private _df: DialogFactory ){ this.therapistId = this._params.data['therapistId']; console.log(this.therapistId); this.getTherapistInfo(this.therapistId); this.requests = this._af.database.list('/requests'); this.getCurrentLocalDateTime() } say(): string { return "foo"; } ngAfterViewInit() { this._df .createDialog(MyDialogComponent, {message: 'This is just an example.'}) .then(({dialog, destroy}) => { this._dialog = dialog; this._destroyDialog = destroy; }); } showDialog() { console.log("きたよ"); if (this._dialog) { this._dialog.show(); } } getTherapistInfo(therapistId: any) { this.therapistObject = this._af.database.object('/therapists/' + therapistId); this.therapistObject.subscribe(response => { this.therapistGender = response['gender']; }); } getCurrentLocalDateTime() { this.defaultDateTime = new Date(Date.now()); this.defaultDateTime.setUTCHours(this.defaultDateTime.getUTCHours() + 9); this.defaultPlusDateTime = new Date(Date.now()); this.defaultPlusDateTime.setUTCHours(this.defaultPlusDateTime.getUTCHours() + 10); this.defaultDateTime.setSeconds(0,0); this.defaultPlusDateTime.setSeconds(0,0); this.startingTime = this.getISOStringWithoutSecsAndMillisecs1(this.defaultDateTime.toISOString().split('T')); this.endTime = this.getISOStringWithoutSecsAndMillisecs1(this.defaultPlusDateTime.toISOString().split('T')); } getISOStringWithoutSecsAndMillisecs1(dateAndTime) { const time = dateAndTime[1].split(':') return dateAndTime[0]+' '+time[0]+':'+time[1] } getUserProfileImageUrl(request: any) { let id: string = request.userId; return "https://firebasestorage.googleapis.com/v0/b/relappi-5ca22.appspot.com/o/images%2Fusers%2F" + id + "_users.jpg" + "?alt=media&token=f97839fa-804a-4497-81ac-44ecc4ef64cc" } pushToDetail(request: any){ this._navigator.element.pushPage(UserDetailComponent, { data: { therapistId: this.therapistId, request: request, } }); } }
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/16 13:36
2016/12/16 18:19