我試圖從使用click
事件的json響應中獲得標題,當我單擊按鈕時,我設法獲取所有標題,但是如何獲取特定標題取決於用戶點擊了button
或a href
?angular2獲取json格式的字符串,然後點擊
modalGetTitle(title) {
this.http.get('../../xmlConf/dashboard_journey.json')
.map((res:Response) => res.json())
.subscribe(data => {
if(data) {
var jsonObj = JSON.parse(JSON.stringify(data));
this.oJourney = jsonObj.o.journey;
this.getJourneyTitle = this.oJourney;
for (var i = 0; i < this.getJourneyTitle.length; i++) {
var element = this.getJourneyTitle[i];
console.log(element.title);
}
}
});
};
像 如果我點擊<button (click)="modalGetTitle()">Title 1</button>
,我想呈現JSON響應標題在<span>
,因此該按鈕只是觸發功能。
大氣壓我只是從響應得到最後一個項目:
<a href="javascript:;" (click)="modalGetTitle()" class="float-shadow" *ngIf="journey.journey_url == 'javascript:;' ">{{journey.title}} </a>
但這隻會讓我無論在硬編碼的範圍內?我打算在中顯示標題。按鈕只是爲了觸發該功能。 – nCore
在這種情況下,你可以在這樣的函數集中標題 var title = document.getElementById('spanId')。innerHTML; –
如果語句沒有返回任何內容,我認爲我不需要執行文檔獲取,因爲我可能只是在視圖中{{title}}。看到我對Thierry的回覆。 – nCore