Ionic 3-从组件发送数据到服务 [英] Ionic 3 - Sending data to service from component
问题描述
我正在尝试从我的组件中发送一个对象,该对象是从用户的输入到我所拥有的服务的.
I am trying to send an object from my component which is from the input of the user to the service I have.
这是我的代码.
Sample.html
Sample.html
<ion-item>
<ion-label color="primary">Subject</ion-label>
<ion-select [(ngModel)]="attendanceSet.subject">
<ion-option *ngFor="let subject of subjects" [value]="subject.title">{{subject.title}}</ion-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label color="primary">Date</ion-label>
<ion-datetime [(ngModel)]="attendanceSet.date" displayFormat="DDD MMM DD" pickerFormat="YYYY MM DD"></ion-datetime>
</ion-item>
<button ion-button block padding (click)="proceed(attendanceSet)">Proceed</button>
Sample.ts
Sample.ts
public attendanceSet = {}
proceed(attendanceSet) {
this.navCtrl.push(CheckAttendancePage, {
setData: attendanceSet
});
}
服务
private attendanceListRef: AngularFireList<Attendance>
public setAttendance = {}
constructor(private db: AngularFireDatabase, private events: Events, public navParams: NavParams, private method: CheckAttendancePage){
this.attendanceListRef = this.db.list<Attendance>('attendance/')
}
addAttendance(attendance: Attendance) {
return this.attendanceListRef.push(attendance)
}
我想做的是从sample.html获取数据,稍后将使用它来定义"attendanceListRef"路径.像这样的出勤/主题/日期"
What I'm trying to do is getting data from the sample.html which I will later use to define the path of the "attendanceListRef" like this "attendance/subject/date"
是否有任何适当或替代的方法来做到这一点?谢谢!
Is there any proper or alternative way to do this? Thanks!
推荐答案
使用Angular将数据发送到 Service
非常简单.在此示例中,我将给您三个选择.
It's quite simple to send data to a Service
with Angular. I'll give you three options in this example.
service.ts
import { Injectable } from '@angular/core';
import { Events } from 'ionic-angular';
@Injectable()
export class MyService {
// option 1: Using the variable directly
public attendanceSet: any;
// option 3: Using Ionic's Events
constructor(events: Events) {
this.events.subscribe('set:changed', set => {
this.attendanceSet = set;
});
}
// option 2: Using a setter
setAttendanceSet(set: any) {
this.attendanceSet = set;
}
}
在您的 Component
中,您可以执行以下操作.
In your Component
you can do the following.
import { Component } from '@angular/core';
import { Events } from 'ionic-angular';
// import your service
import { MyService } from '../../services/my-service.ts';
@Component({
selector: 'something',
templateUrl: 'something.html',
providers: [MyService] // ADD HERE -> Also add in App.module.ts
})
export class Component {
newAttendanceSet = {some Object};
// Inject the service in your component
constructor(private myService: MyService) { }
proceed() {
// option 1
this.myService.attendanceSet = this.newAttendanceSet;
// option 2
this.myService.setAttendanceSet(this.newAttendanceSet);
// option 3
this.events.publish('set:changed', this.newAttendanceSet);
}
}
基本上,我建议您选择选项1或2.在我看来,3被认为是不好的做法,但是它仍然可以工作,并且如果您需要在多个服务/组件上更新价值,有时可以提供极大的灵活性.
Basically I'd recommend option 1 or 2. 3 is considered bad practice in my eyes but it still works and sometimes allows for great flexibility if you need to update your value over multiple services/components.
这篇关于Ionic 3-从组件发送数据到服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!