0

I am new to Angular2 and my trying my hand using @Input but I am not able to proceed because of the below issue. After the @Input the component does not proceed further. I have verified in chrome developer tools and I see that execution goes outside the class immediately after the @Input

import {Component, Input, OnInit} from 'angular2/core';
    import {Http, HTTP_PROVIDERS} from 'angular2/http';
    import 'rxjs/Rx';

    var availableJobs = [];


    @Component({
      selector: 'job-categories',
      templateUrl:'templates/job.categories.html',
      providers:[HTTP_PROVIDERS]

    })

    export class JobCategories{

      @Input('rows') rows: string;
      @Input('cols') columns: string;


constructor(http: Http){
        http.get('appjs/dummyjson.json').map(res => res.json()).subscribe(
            (data) => {
              availableJobs = data;
              console.log(availableJobs);
            });
        }
    }

Could someone please help me overcome.

The HTML tag is

8
  • What does "the component does not proceed further." and "execution goes outside the class" mean? I can't see anything being obviously wrong. Commented Apr 25, 2016 at 6:53
  • True. Hence, I came to StackOverflow for help. What it means is, F10 on chrome dev tools will not take the execution to next line but it takes me to the end of the class Commented Apr 25, 2016 at 6:57
  • What behavior do you expect? Commented Apr 25, 2016 at 6:58
  • I would want the HTTP call happen and see the data in the JSON. Any dummy data is fine for testing Commented Apr 25, 2016 at 6:59
  • 2
    The inputs and the Http call don't seem to be related. The Http call should be made before the inputs are processed. Input values are assigned when the ngOnChanges() lifecycle callback is called the first time (just before ngOnInit() is called. The constructor is executed before any lifecycle callback is called. Commented Apr 25, 2016 at 7:01

3 Answers 3

2

I would see a problem in your code. You forgot the this keyword within the subscribe callback:

constructor(http: Http){
    http.get('appjs/dummyjson.json').map(res => res.json()).subscribe(
        (data) => {
          this.availableJobs = data; // <-----
          console.log(this.availableJobs);
        });
    }
}

This way you will be able to see / use the availableJobs property in the template of the component...

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

4 Comments

Maybe OP didn't forget it, it's defined as local variable var availableJobs = []; not as instance property.
Yes but I guess that he wants to use availableJobs in the component template ;-)
@dfsq Yes, I did declare as a local variable. Thanks for the efforts. The issue is resolved
Great! What was the problem?
0

Seems like the problem is in your html(template). and the dev tools will not brings you into the subscribe method(async call), for that please keep a debugger; inside subscribe.

Would like to see your Html.

should be used like

<job-categories [rows]="someRowVariable" [cols]="someColVariable" ></job-categories>

Comments

0

this happened because of defining a availableJobs variable outside a class . Define it within class name JobCategories

import {Component, Input, OnInit} from 'angular2/core';
    import {Http, HTTP_PROVIDERS} from 'angular2/http';
    import 'rxjs/Rx';




    @Component({
      selector: 'job-categories',
      templateUrl:'templates/job.categories.html',
      providers:[HTTP_PROVIDERS]

    })

    export class JobCategories{
      var availableJobs = [];  //Define within a class
      @Input('rows') rows: string;
      @Input('cols') columns: string;


constructor(http: Http){
        http.get('appjs/dummyjson.json').map(res => res.json()).subscribe(
            (data) => {
              availableJobs = data;
              console.log(availableJobs);
            });
        }
    }

Comments

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.