2017-09-30 5 views
0

.TSのdoSomething(呼び出すときに.TSクラスを仮定メッセージプロパティを動的に渡してコントローラのsubTitleプロパティに警告する方法はありますか?

//imported everything 


@Component({ 
    selector: 'anything', 
    templateUrl: '.html', 

}) 

export class anything{ 

public x: number; 
let message : string; 


    constructor(public nav: NavController, public authData: AuthData,public service: Service) { } 


    async doSomething() { 

     try { 

     await this.authData.Something(this.x); 

} 
catch(error){ 
this.message = 'Sorry, sme error occured' 

this.service.showAlert(); 

}    
    } 

service.ts

//using alert controller code in this class. 

showAlert(){ 
title: 'static', 
subtitle: '', // need to add this message dynamically from the function that called the showAlert() function. 

button: [Okay]; 
} 
//code implemented. 

)、それはcatchブロックでキャッチされた例外をスローとcatchブロックは、アラートコントローラのshowAlert()関数をトリガーします。

質問:showAlert()にsubTitleの値を動的に配置して、関数と呼ばれる関数に従ってメッセージを送信できるようにするにはどうすればよいですか?

注::構文エラーとタイプミスとインポートを無視してください。

答えて

1

1.あなたはまた、あなたのサービスのメソッドは、あなたがそれをそうのような引数を渡している知っている必要があるだろう

@Component({ 
    selector: 'anything', 
    templateUrl: '.html', 

}) 

export class anything{ 

public x: number; 
let message : string; 


constructor(public nav: NavController, public authData: AuthData,public service: Service) { } 


async doSomething() { 
     try { 
     await this.authData.Something(this.x); 
     } 

catch(error){ 
this.message = 'Sorry, sme error occured' 
//Add message as args to the calling method 
this.service.showAlert(this.message); 

}    
} 

2.関数を呼び出すあなたにパラメータを渡す必要があります。

//Receive args in service.Good idea to type 
showAlert(receivedMessage:string){ 
title: 'static', 
subtitle: receivedMessage, // Add received message to alert body 

button: [Okay]; 
} 
関連する問題