我正在将数据传递给" add-item.ts"到" home.ts",然后将数据推入数组。但是,一旦我导航到add-item.html,从" home.ts"中删除了先前的数据,或者可能再次加载构造函数。我**无法从" add-item.ts"的" home.ts" **中推出数据。请尽快建议我................................................................................................................
我的" add-item.html":
export class AddItemPage {
posts:Posts[];
public showPage: boolean;
dateExp : string;
check : string;
expense :string ;
currency : string;
amount :any;
billToggl :boolean;
msg : String ;
myDate: String = new Date().toISOString().substring(0, 10);
constructor(public navCtrl: NavController, public navParams: NavParams, public viewCtrl : ViewController,
public modalCtrl: ModalController,public toastCtrl: ToastController,private _form: FormBuilder,
private postsService: PostsService , public events: Events){
// this.postsService.getPosts().subscribe(posts => {
// console.log(posts);
// this.posts=posts;
// });
this.dateExp = "";
this.check = "Bus_fare";
this.amount = 0 ;
this.currency = "INR";
this.expense = "";
}
closeModel()
{
this.viewCtrl.dismiss();
}
isValid()
{
if(this.dateExp === "")
{
let toast = this.toastCtrl.create({
message: 'please choose date',
duration: 2000,
position: 'bottom',
cssClass: 'toastColor'
});
toast.present();
return false;
}
if(this.dateExp > this.myDate)
{
let toast = this.toastCtrl.create({
message: 'Future Date is not allowed',
duration: 2000,
position: 'bottom',
cssClass: 'toastColor'
});
toast.present();
return false;
}
if( this.check === "")
{
//console.log(this.check)
let toast = this.toastCtrl.create({
message: 'select Description feild ',
duration: 1000,
position: 'bottom',
cssClass: 'toastColor'
});
toast.present();
console.log( 'select Description feild' );
return false;
}
if( this.check === 'other')
{
//console.log(this.check)
let toast = this.toastCtrl.create({
message: 'please fill Other detail',
duration: 1000,
position: 'bottom',
cssClass: 'toastColor'
});
toast.present();
console.log( 'select Description feild' );
return false;
}
if( this.currency === "")
{
let toast = this.toastCtrl.create({
message: 'choose currency type',
duration: 1000,
position: 'bottom',
cssClass: 'toastColor'
});
toast.present();
console.log( 'choose currency' );
return false;
}
if( this.amount === 0 || this.amount === "" )
{
let toast = this.toastCtrl.create({
message: 'fill amount',
duration: 1000,
position: 'bottom',
cssClass: 'toastColor'
});
toast.present();
console.log( 'fill amount' );
return false;
}
return true;
}
launchpage()
{
if(this.isValid())
{
this.showPage = true;
let data ={
date : this.dateExp,
details :this.check,
amount : this.amount,
bill : this.billToggl,
currency : this.currency,
expense : this.expense,
showPage : this.showPage,
};
this.navCtrl.push(HomePage,data);
// this.events.publish('myEvent', this.dateExp,this.check,this.amount,this.billToggl,this.currency,this.description,this.showPage);
let toast = this.toastCtrl.create({
message: 'item is added',
duration: 1000,
position: 'bottom',
cssClass: 'saveToast'
});
toast.present();
// this.navCtrl.pop();
}
}
}
interface Posts
{
id : number;
title : string;
body : string;
}
和我的" home.ts"是:
export class HomePage {
additem= AddItemPage;
public isToggled: boolean;
public showPage : boolean = true;
static arrs : arr[];
date :any;
details :any;
amount :any;
bill :any;
expense : string ;
value :any;
currency :string ;
costCenter :string;
wbs :string;
Approver :string;
billClient :boolean;
exception :string ;
Particulars : string ;
constructor(public navCtrl: NavController, public modalCtrl: ModalController, public navParams :NavParams ,
public alertCtrl: AlertController,public toastCtrl: ToastController,public http:Http)
{
HomePage.arrs=[{
// costCenter :this.costCenter,
// wbs :this.wbs,
// Approver :this.Approver,
// billClient :this.billClient,
// exception :this.exception ,
date:this.date,
details :this.details ,
currency : this.currency,
amount : this.amount,
bill : this.bill,
expense : this.expense,
}];
HomePage.arrs.splice(HomePage.arrs.indexOf(HomePage.arrs[0],1));
this.date = this.navParams.get('date');
this.details = this.navParams.get('details');
this.amount = this.navParams.get('amount');
this.bill = this.navParams.get('bill');
this.showPage = this.navParams.get('showPage');
this.currency = this.navParams.get('currency');
this.expense = this.navParams.get('expense');
this.costCenter = "Project1";
this.wbs ="";
this.Approver ="Manager1";
this.billClient =false;
this.exception = "";
this.Particulars = "";
this.abc(this.date,this.details, this.currency,this.amount,this.bill,this.expense);
this.exception = "";
}
abc(a,b,c,d,e,f)
{
HomePage.arrs.push({
// costCenter :this.costCenter,
// wbs :this.wbs,
// Approver :this.Approver,
// billClient :this.billClient,
// exception :this.exception ,
date:this.date,
details :this.details ,
currency : this.currency,
amount : this.amount,
bill : this.bill,
expense : this.expense
});
console.log(HomePage.arrs);
}
public notify() {
console.log("Toggled: "+ this.isToggled);
}
isValidHome()
{
console.log(this.isToggled);
if(this.isToggled == true)
{
if(this.exception === "")
{ let toast = this.toastCtrl.create({
message: 'Please Fill Exception feild',
duration: 1000,
position: 'bottom'
});
toast.present();
return false;
}
}
return true ;
}
Send()
{
if(this.isValidHome()){
let alert = this.alertCtrl.create({
title: 'Thank you ?',
message: 'Your voucher no :1234',
inputs: [
{
name: 'recurrent',
label :'Add to recurrent',
type : "checkbox",
value :"true"
}
],
buttons: [
{
text: 'Ok',
handler: () => {
console.log(this.wbs);
console.log(this.costCenter);
console.log(this.Approver);
console.log(this.billClient);
console.log(this.exception);
console.log(this.Particulars);
}
},
]
});
alert.present();
}
}
Change(value)
{
console.log(value);
if(value != "" )
{
this.costCenter = "";
}
else
{
this.wbs = "";
}
}
Change1(value)
{
console.log(value);
if(value != "" )
{
this.wbs = "";
}
else
{
this.costCenter = "";
}
}
}
interface arr{
// costCenter :string;
// wbs :string;
// Approver :string;
// billClient :boolean;
// exception :string ;
date:String;
details :String ;
currency : String ;
amount : number;
bill : any;
expense :string ;
}
使用CLI命令 IONIC G提供者ProvidErname
创建提供商将您的价值存储在这样的供应商中,
import { Injectable } from '@angular/core';
@Injectable()
export class Provider {
public addedItems:any;
constructor() {
}
addedItems(data) {
return this.addedItems=data;
}
}
并将此提供商导入您的家庭组件以检索添加值