2016-07-25 109 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中 –

相关问题