我正在尝试使用 click
从 json 响应中获取标题事件,当我单击按钮时,我设法获取所有标题,但是如何根据 button
获取特定标题或a href
用户点击了?
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>
我想在 <span>
中呈现 json 响应标题,因此该按钮只是触发该功能。
Atm 我只从响应中获取最后一项:
<a href="javascript:;" (click)="modalGetTitle()" class="float-shadow" *ngIf="journey.journey_url == 'javascript:;' ">{{journey.title}} </a>
最佳答案
如果我理解你的问题,你可以试试这个。
<button ng-click="modalGetTitle()"><span id="spanId">Title</span></button>
在 Controller 中你可以定义函数
$scope.modalGetTitle = function(){
var title = document.getElementById('spanId').innerHTML
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];
if(element == title){
console.log(element.title);
}
}
}
});
}
关于javascript - Angular2 单击时获取 json 格式的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38852201/