0

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>
3
  • Looks a bit weird. The http.get(...) code is executed as first thing when the component is created, but the onKey(...) is only executed later when a key is pressed. That can't work. You need to move the http.get... to onKey() where values is updated. Commented Jul 20, 2016 at 18:24
  • I tried to place the constructor in the onKey function but it gives an error saying "cannot find constructor", I am new to typescript and it has some weird gotchas Commented Jul 20, 2016 at 18:37
  • 1
    I didn't mean the constructor. I meant the content of the constructor. You can't move the constructor inside another method. Commented Jul 20, 2016 at 18:44

2 Answers 2

2

This is a wrong syntax to append value to the stream. this.values = value + '';

What it is doing is overriding old values again and again. Therefore correct code is this.values = this.values + value + '';

Sign up to request clarification or add additional context in comments.

2 Comments

Great catch! I don't get how this is related to stream. I guess you meant string.
Unfortunately, that appends to the {{values}} but won't change when deleted and it doesn't pass the value to the url
0

As said by Günter Zöchbauer, the constructor is performed at the time of construction of the component, and you can not use it to update values. You have to do something like this:

export class AppComponent {

  ...

  values = '';

  onKey(value: string) {

    this.values += value + '';

    getData(); // call getData method
  }

  people: Array<Object>;

  constructor(public http: Http) {

  }

  getData(){

      this.http.get('http://myurl.com/json_feed.php?q=' + this.values)
      ...

    })
  }

}

Also please note your http request will not work if you do not provide HTTP_PROVIDERS when you bootstrap the application in your main.ts.

4 Comments

I finally got it to work constructor(http: Http) { this.http = http; } and getData(){ this.http.get('myurl.com/json_feed.php?q=' + this.values) ... })
Ok, I've updated the answer also showing the correct use of the http provider. Greetings
Hi, we just need this.http=http; in the constructor or it won't work constructor(public http: Http) { this.http = http; }
If you use: constructor(public http: Http), you can avoid using: this.http = http;. It is the short way to instantiate the service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.