[英]Global object in angular2
How can I create a global object in angular2. 如何在angular2中创建全局对象。 I am collecting data from one page to another means page by page(may be for 4-5 pages) using navParams in ionic2.
我正在使用ionic2中的navParams逐页(可能是4-5页)从一页到另一页的数据收集。 but want to store it in global object & finally submit it.
但想将其存储在全局对象中并最终提交。 I have tried it using global provider but haven't got most of it & not getting any clue also.
我已经使用全球提供商尝试了它,但是还没有得到大多数,也没有任何线索。 Please any suggestion.
请提出任何建议。
As with my question ionic community suggest to use global provider. 正如我的问题一样,离子社区建议使用全球供应商。 i did use global provider to hold my 7 step form data and final submit on last step.
我确实使用了全局提供程序来保存我的7步表单数据,并在最后一步最后提交。
i used with ionic 3. provider code: 我与ionic 3一起使用。提供者代码:
import { Injectable } from '@angular/core';
@Injectable()
export class NcConnectionProvider {
public statecode:any;
public districtcode:any;
constructor() {
//console.log('Hello NcConnectionProvider Provider');
}
}
Page.ts 页面
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { NcConnectionProvider } from '../../providers/nc-connection/nc-connection';
@IonicPage()
@Component({
selector: 'page-nc-connection',
templateUrl: 'nc-connection.html',
})
export class NcConnectionPage {
public distbform:FormGroup;
public statedata:any;
public districtdata:any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
public ncdatashare: NcConnectionProvider) {
}
locateDistb(form: NgForm){
this.ncdatashare.statecode = this.distbform.value.state;
this.ncdatashare.districtcode = this.distbform.value.district;
}
}
you can inject provider to multiple pages as you want and access your global variable/object. 您可以根据需要将provider注入多个页面并访问全局变量/对象。 like in example value set to
this.ncdatashare.statecode = this.distbform.value.state;
例如在示例
this.ncdatashare.statecode = this.distbform.value.state;
值设置为this.ncdatashare.statecode = this.distbform.value.state;
now you can access this.ncdatashare.statecode
to other page but provider must inject there. 现在,您可以访问
this.ncdatashare.statecode
到其他页面,但提供程序必须在此处注入。
Make sure to provide some sample code to elaborate the problem better. 确保提供一些示例代码以更好地阐述问题。 Moreover, following is the way you can follow to collect the data in single object.
此外,以下是您可以遵循的在单个对象中收集数据的方式。
Create a service with a variable and its getter setter. 创建一个带有变量及其getter设置程序的服务。 Inject this service in your component and set the data wherever needed and later use its get method to collect it back again.
将此服务注入您的组件中,并在需要的地方设置数据,然后使用其get方法再次将其收集回来。
@Injectable()
export class DataService{
private _data: any;
get data() {
return this._data;
}
set data(data: any) {
this._data = data;
}
}
and in side your component 在你的组件旁边
@Component({
// configurations
})
export class LoginComponent {
constructor(public dataService: DataService) {
this.dataService.data = { userAge: 37 }
}
}
and where you need to submit this data, just use the getter to collect it back. 在需要提交此数据的地方,只需使用吸气剂将其收集回来即可。
let finalData = this.dataService.data;
Not sure about your use case. 不确定您的用例。 But I hope it might help you.
但我希望它能对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.