2017-07-12 20 views
0

我試圖在離子2警告框中顯示選定的索引值。但我沒有得到正確的方式如何顯示在離子的提示。如何使用離子2框架在警報框中顯示選定的索引值

這是home.ts

import { Component } from '@angular/core'; 
import { NavController } from 'ionic-angular'; 
import {AlertController} from 'ionic-angular'; 

@Component({ 
selector: 'page-home', 
templateUrl: 'home.html' 
}) 
export class HomePage { 
companies: Array< {name: string, code: number}> 
constructor(public navCtrl: NavController, public alertController: 
AlertController) { 
    this.companies = [ 
     {name: 'Microsoft', code: 1}, 
     {name: 'Apple', code: 2}, 
     {name: 'Google', code: 3}, 
     {name: 'Oracle', code: 4}, 
     {name: 'IBM', code: 5}, 
    ]; 
} 

delete(no) { 
    let alert = this.alertController.create({ 
     title: "Example", 
     subTitle: "Example SubTitle" + {{no}}; 
     buttons: ["OK"] 
    }); 

    alert.present(); 
    (this.companies).splice(no, 1); 
    } 

} 

在上面的刪除功能delete(no)我傳遞no作爲參數刪除功能,我需要在警告對話框顯示相同的值。

+0

爲什麼你在刪除方法周圍'no'大括號? – CuriousMind

+0

沒有任何價值,我必須在警示框中顯示 – user993164

+0

然後只需連接它,不需要放大括號。 – CuriousMind

回答

0

很好地爲此創建共享提供程序。

shared.provider.ts:

public Alert = { 
    confirm: (msg?, title?, no?) => { 
     return new Promise((resolve, reject) => { 
     let alert = this._alert.create({ 
      title: title || 'Confirm', // `Example SubTitle ${no}` 
      message: msg || 'Do you want continue?', 
      buttons: [ 
      { 
       text: 'Cancel', 
       role: 'cancel', 
       handler:() => { 
       reject(false); 
       } 
      }, 
      { 
       text: 'Ok', 
       handler:() => { 
       resolve(true); 
       } 
      } 
      ] 
     }); 
     alert.present(); 
     }); 

    }, 
    alert: (msg, title?) => { 
     let alert = this._alert.create({ 
     title: title || 'Alert', 
     subTitle: msg, 
     buttons: ['Dismiss'] 
     }); 

     alert.present(); 
    } 
    } 

調用警報確認:

 import { SharedProvider } from '../../providers/shared.provider'; 
    this.shared.Alert.confirm('Would you like to delete?', 'Confirm', 2).then((response) => { 
      console.log('confirmed'); 
     }, err => { 
      console.error('rejected'); 
     });