简体   繁体   English

Angular2 @Input问题

[英]Angular2 @Input issue

I am new to Angular2 and my trying my hand using @Input but I am not able to proceed because of the below issue. 我是Angular2的新手,我尝试使用@Input进行操作,但是由于以下问题,我无法继续。 After the @Input the component does not proceed further. @Input之后,该组件不再继续。 I have verified in chrome developer tools and I see that execution goes outside the class immediately after the @Input 我已经在chrome开发人员工具中进行了验证,并且看到@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 HTML标签是

I would see a problem in your code. 我会在您的代码中看到一个问题。 You forgot the this keyword within the subscribe callback: 您在subscribe回调中忘记了this关键字:

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... 这样,您将可以在组件模板中查看/使用availableJobs属性。

Seems like the problem is in your html(template). 似乎问题出在您的html(template)中。 and the dev tools will not brings you into the subscribe method(async call), for that please keep a debugger; 开发工具不会将您带入Subscribe方法(异步调用),为此,请保留调试器; inside subscribe. 内部订阅。

Would like to see your Html. 希望看到您的HTML。

should be used like 应该像

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

this happened because of defining a availableJobs variable outside a class . 发生这种情况是因为在类外部定义了availableJobs变量。 Define it within class name JobCategories 在类名称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);
            });
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM