2016-07-25 110 views

回答

1

我認爲你有兩個解決方案來解決它。

  1. 如果要將參數值從FirstPage傳遞給SecondPage,則可以使用NavParams。

在第一頁:

this.navController.push(SecondPage, { yourParameterName: yourParameterValue }); 

在第二頁:

onPageDidEnter() { 
    this.yourClassVariable = this.navParams.get('yourParameterName'); 
} 

請注意,如果你想通過從SecondPage東西FIRSTPAGE過,你可以傳遞一個回調的參數並在第二頁中調用一個在第一頁中指定一個值,然後再執行後退操作(this.navController.pop())

2.You還可以解決價值觀的共享通的注射服務的幫助下

@Injectable() 
export class MyService { 
    private string $value; 

    setValue(newValue:string) { 
     this.$value = newValue; 
    } 

    getValue():string { 
     return this.$value; 
    } 
} 

然後在這兩個頁面中,您可以添加服務的供應商並設置/讓你在有趣的價值

@Component({ 
    providers: [MyService] 
}) 
export class FirstPage { 
    constructor(private myService:MyService) { 
    } 

    private anyWhere() { 
     this.myService.setValue('myValue'); 
     this.myService.getValue(); 
    } 
} 
+0

小的修正:'MyService'不應該被添加爲兩個頁面的提供者,因爲那時它的一個新的實例將被創建並不會出現數據的任何持久性。相反,它應該只是頁面雙親的提供者,或者如果一頁是第二頁的父頁面 - 它應該是第一頁的提供者。 – yarons

+0

thx爲加載項,同意。就我個人而言,我將所有提供者列在主類app.ts中 –

相關問題