I am trying to pass a value from the html file to my component and then add it to my json feed url.
I have managed to get it to the component and print to the html file, but U can't get it appended to the end of my json feed url.
This component and html file is straight from anguler2 cli
app.component.ts
import { Component } from '@angular/core';
import {Http} from '@angular/http';
import {HTTP_PROVIDERS} from '@angular/http';
@Component({
moduleId: module.id,
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class AppComponent {
title = 'app works!';
values = '';
onKey(value: string) {
this.values = value + ''; // pass this value to the url below
}
people: Array<Object>;
constructor(http:Http) {
http.get('http://myurl.com/json_feed.php?q=' + this.values) // get the value from above
.subscribe(res => {
this.people = res.json();
})
}
}
app.component.html
<h1>
{{title}}
</h1>
<input #box (keyup)="onKey(box.value)">
{{values}}
<ul *ngFor="let person of people">
<li class="text">
{{person.model}}
</li>
</ul>
http.get(...)code is executed as first thing when the component is created, but theonKey(...)is only executed later when a key is pressed. That can't work. You need to move thehttp.get...toonKey()wherevaluesis updated.