Ionic 2 / Angular 2:如何从Ionic 2警报上的超链接或按钮发出点击事件?

时间:2017-02-04 12:04:21

标签: angular hyperlink ionic2

我的家。:

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

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController, 
              public alertCtrl: AlertController) {
  }


  ShowAlert(){
    let alert = this.alertCtrl.create({
        title: '<b>Information</b>',
        subTitle: `<a href="#" (click)="launchUrl('https://www.google.com')">Open Google</a>`,
        buttons: ['Close']
      });

    alert.present();
  }

  launchUrl(url){
    console.log(url);
  }
}

我的HTML看起来像:

<button  ion-button icon-only color="light" style="float: right" (click)="ShowAlert()">
Show Alert
</button>

我确实成功触发了警报。

现在,当我点击超链接:Open Google时,没有任何反应。我想点击超链接调用launchUrl()函数。

  launchUrl(url){
    console.log(url);
  }

一旦我能够调用launchUrl(),之后我希望如下所示修改它以使用cordova插件调用InAppBrowser。

launchUrl(url){
        this.platform.ready().then(() => {
            cordova.InAppBrowser.open(url, "_system", "location=true");
        });
    }

但是现在我甚至无法调用基本的launchUrl()函数。 我尝试了(click)="launchUrl('https://www.google.com')"(click)="this.launchUrl('https://www.google.com')",但没有成功。

请告知。

1 个答案:

答案 0 :(得分:1)

http://dmidland.byethost5.com/dmidlandProto2/step4brad.php删除了警报的HTML。如果要保留,则应使用 ShowAlert() { let alert = this.alertCtrl.create({ title: 'Information', message: 'Search Google?', buttons: [ { text: 'Cancel', role: 'cancel', handler: () => { console.log('Cancel clicked'); } }, { text: 'Ok', handler: () => { console.log('start going to google...'); } } ] }); alert.present(); } 函数将其添加到“受信任列表”。

但这不会解决你的问题,因为你假设Angular2绑定将出现在Alert的字幕上。警报不是为您尝试使用它而设计的。

参考Angular2 Sanitiser,您可以看到如何使用Button和Handler这样做。

您可能需要稍微更改设计才能使用Alert开箱即用。例如,使用此

import sys

from PyQt4.QtCore import QTimer
from PyQt4.QtGui import (
QApplication,
QWidget,
QVBoxLayout,
QPushButton,)

class MyApp(QWidget):

    def __init__(self,*args,**kwargs):
        QWidget.__init__(self,*args,**kwargs)

        self.layout = QVBoxLayout(self)

        self.button = QPushButton('Start timer')
        self.button.clicked.connect(self.start_timer)

        self.button1 = QPushButton('Stop timer')
        self.button1.clicked.connect(self.stop_timer)

        self.layout.addWidget(self.button)
        self.layout.addWidget(self.button1)
        self.timer = QTimer()
        self.timer.timeout.connect(self.hello)
        self.timer.setSingleShot(False)


    def start_timer(self):
        self.timer.start(1000)

    def stop_timer(self):       
        self.timer.stop ()
        print('timer stop')

    def hello(self):
        b=1   
        print(a)
        a.append(b)
        print(a)
        if len(a) == 5:
            self.timer.stop ()
            print('timer stop')


a=[]
# Create QApplication and QWidget
qapp = QApplication(sys.argv)  
app = MyApp()
app.show()
qapp.exec_()