2017-08-25 98 views
1

有什麼方法可以使用方法將數據{{error.value}}發送到另一個頁面?使用服務在組件之間共享數據 - IONIC 2,Angular 2

這是我的代碼

<ion-row *ngFor="let errors of adp_principal_menuRS"> 
 
      <ion-col class="info-col" col-4> 
 
      <button ion-button color="primary" small (click)="goToErrors(errors.event)"> 
 
       {{errors.event}} 
 
      </button> 
 
      </ion-col> 
 
</ion-row>

goToErrors(menu: string){ 
 
    console.log(menu); 
 
    this.navCtrl.push(AdpDetailPage, { 
 

 
    }); 
 
    }

我想了{{errors.event}}值發送到另一個頁面的goToErrors()方法。

謝謝!

編輯:我只是實現我想要的。我編輯了代碼

+0

'goToErrors()'將導航到另一個頁面? – Rajez

+0

是的,我想在該頁面中使用errors.event的值。 – bECkO

回答

1

你爲什麼不發使用navParam價值?

goToErrors(menu: string){ 
    console.log(menu); 
    this.navCtrl.push(AdpDetailPage, { 
     errorEvent: menu // <------------------------- Add this line 
    }); 
    } 

而在你AdpDetailPage

export class AdpDetailPage{ 
constructor(public navParams: NavParams){ 

    errorEvent = this.navParams.get('errorEvent'); 
    console.log("errorEvent= ", errorEvent); 
} 
} 
+0

謝謝,我有這樣的代碼,它的工作原理。 – bECkO

1

使用事件發射器。

//Home component.ts import { Events } from 'ionic-angular'; constructor(public events: Events) {} directioChange(user) {this.events.publish('directiochanged', 'true');} //App.component.ts constructor(public events: Events) { events.subscribe('directiochanged', (direction) => { this.isRtl = direction;console.log(direction);});} 
+0

查看編輯答案 –

+0

根據您的要求更改 –

3

可以通過服務在組件之間使用BehaviorSubject共享數據。 下面是一個例子:

// service.ts 
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; 

@Injectable() 
export class ShareService { 
    private errorSource = new BehaviorSubject<any>(null); 
    error$ = this.errorSource.asObservable(); 

    setError(error: any){ 
     this.errorSource.next(error); 
    } 

設置使用setError方法和訂閱在error componenterrorerror eventparent component

// error component.ts 
    constructor(share: ShareService) { 
     share.error$.subscribe(Err => this.error = Err); 
+0

我有點困惑。我的數據來自一項服務,我需要創建另一項服務來將我需要的數據分享給另一個頁面? – bECkO

+0

新服務的需求取決於服務的範圍。如果數據共享組件和數據獲取服務都在同一模塊中,則不需要新服務。'errorSource BehaviorSubject'可以在同一服務中實現,用於獲取數據。 – Rajez

1

我生成了一個Plunker,希望與您正在嘗試做的事情相匹配。

https://plnkr.co/edit/MNqpIqJjp5FN30bJd0RB?p=preview

服務

import { Injectable } from '@angular/core'; 

@Injectable() 
export class ErrorService { 
    errorInfo: string; 
} 

組件

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
     <h2>Hello {{name}}</h2> 
     <button (click)="goToErrors()">{{errors.event}} </button> 
     <router-outlet></router-outlet> 
    </div> 
    `, 
}) 
export class App { 
    name:string; 
    errors = { event: 'Test Error', otherInfo: 'Test Info' }; 

    constructor(private errorService: ErrorService, private router: Router) { 
    this.name = `Angular! v${VERSION.full}` 
    } 

    goToErrors(): void { 
    // Code to navigate to the other component 
    this.errorService.errorInfo = this.errors.event; 
    this.router.navigate(['/a']); 
    } 
} 
+0

我要試試這個,謝謝。 – bECkO

相關問題