nCore nCore - 3 months ago 11
jQuery Question

angular2 get string in json format on click

I'm trying to get the title from json response using a

click
event, I manage to get all the titles when I click on the button, but how can I get specific title depending on the
button
or
a href
the user clicked?

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);
}
}

});
};


Like
if I click on
<button (click)="modalGetTitle()">Title 1</button>
and I want to render the json response title in a
<span>
, therefore the button is just to trigger the function.

Atm I'm only getting the last item from the response:

<a href="javascript:;" (click)="modalGetTitle()" class="float-shadow" *ngIf="journey.journey_url == 'javascript:;' ">{{journey.title}} </a>

Answer

you can try this, if i'm understand your question.

<button ng-click="modalGetTitle()"><span id="spanId">Title</span></button>

in controller you can define function

          $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);
        }
                        }      
                    }
                });
            }
Comments