如何使用JavaScript函数打开API网址,如何将变量从html传输到ts文件

问题描述 投票:-1回答:1

我正在为当前项目使用newsapi。使用html页面本身很容易打开“ data.url”。

[href]='data.url'

我的挑战是尝试使用一个函数打开url,因为仅粘贴'data'url'作为要由InAppBrowser插件打开的链接会导致我进入'localhost:8010 / data.url'。这是我以前的代码。

    <ion-item *ngFor="let data of fetchedData?.articles; index as i" [href]="data.url" target="_blank" >

这是我的新代码。 HTML

    <ion-item *ngFor="let data of fetchedData?.articles; index as i" (click)="openPage()" >

。ts

 openPage() {
  this.fetchedData = data
  this.iab.create('data.url', '_blank');

  }
javascript html api ionic4 inappbrowser
1个回答
0
投票

我现在已经知道答案是这个

    <ion-item *ngFor="let data of fetchedData?.articles; index as i" (click)="openPage(data.url)" >

代替此

    <ion-item *ngFor="let data of fetchedData?.articles; index as i" (click)="openPage()">

然后在.ts文件中,我打算写...

 openPage(url) { // here data.url is assigned to 'url'
  this.iab.create(url, '_blank'); //'url' is called here to be opened in-app

  }
javascript

我希望这可以帮助遇到相同问题的人

© www.soinside.com 2019 - 2024. All rights reserved.