2017-05-10 1 views
1

TypeScript-Angular 4 응용 프로그램에서 this.array 변수를 가져올 수 없습니다.TypeScript-Angular 4 응용 프로그램에서 this.array 변수를 가져올 수 없습니다.

여기서 은 정의되지 않았으므로 this.services.push은 오류를 발생시킵니다. 당신이 this을 둘러싸하면 해당 기능 "예"

권장 독서를 참조 할 function를 사용하는 경우

export class ServersComponent implements OnInit { 
    //Initializing the typescript array 
    services: ServerProperties[] = []; 
    jQuery.each(array, function(key, value) { 
     //Using this.services to push the data which throws error as this.services undefined. 
     //Trying to push the object created 
     this.services.push(new ServerProperties(JSON.stringify(value.serviceName), JSON.stringify(value.state), 
     JSON.stringify(value.domainName), JSON.stringify(value.releaseVersion), 
     JSON.stringify(value.creationDate), JSON.stringify(value.BISERVICEURL), JSON.stringify(value.editionDisplayName))); 
    }); 
} 

export class ServerProperties { 
    public serviceName: string; 
    public state: string; 
    public domainName: string; 
    public releaseVersion: string; 
    public creationDate: string; 
    public BIServiceURL: string; 
    public editionDisplayName: string; 

    constructor(serviceName: string, state: string, domainName: string, releaseVersion: string, creationDate: string, BIServiceURL: string, editionDisplayName: string) { 
    this.serviceName = serviceName; 
    this.state = state; 
    this.domainName = domainName; 
    this.releaseVersion = releaseVersion; 
    this.creationDate = creationDate; 
    this.ServiceURL = BIServiceURL; 
    this.editionDisplayName = editionDisplayName; 
    } 
} 

import { Component, OnInit } from '@angular/core'; 
import { Injectable } from '@angular/core'; 
import { Headers, Http, Response } from '@angular/http'; 
import 'rxjs/Rx'; 
import { Observable } from 'rxjs/Observable'; 
import {JQueryStyleEventEmitter} from 'rxjs/observable/FromEventObservable'; 
import {ServerProperties} from './servers.module'; 
declare var jQuery: any; 


@Component({ 
    selector: 'app-servers', 
    templateUrl: './servers.component.html', 
    styleUrls: ['./servers.component.css'] 
}) 

@Injectable() 
export class ServersComponent implements OnInit { 
    allowNewUser= false; 
    storeValue: ServerProperties[] = []; 
    serverCreationStatus= 'No Server was created! '; 
    serverName= ''; 
    static podServiceName= ''; 
    static podState= ''; 
    static podDomainName= ''; 
    static podReleaseVersion= ''; 
    static podCreationDate= ''; 
    static podBIServiceURL= ''; 
    static podeditionDisplayName= ''; 
    myJSON= ''; 
    //declaring server properties array 
    services: ServerProperties[] = []; 

    Service_Instances= ''; 

    total_services= new Array(); 
    Service_Instances2= ''; 
    serverCreated= false; 
    podPro= {}; 
    dict= {}; 

    constructor(private http: Http) {} 

    setValues(array){ 
    jQuery.each(array, (key, value) => { 
     console.log('Jquery Output 1st' + key + ': ' + value.state); 
     console.log('Jquery Output 1st' + key + ': ' + value.serviceName); 
     console.log('Jquery Output 1st' + key + ': ' + value.domainName); 
     console.log('Jquery Output 1st' + key + ': ' + value.releaseVersion); 
     console.log('Jquery Output 1st' + key + ': ' + value.creationDate); 
     console.log('Jquery Output 1st' + key + ': ' + value.BI_SERVICE_URL); 
     console.log('Jquery Output 1st' + key + ': ' + value.editionDisplayName); 
     // this.total_services.push(value.state); 
     // this.total_services.push(value.serviceName); 
     // this.total_services.push(value.domainName); 
     // this.total_services.push(value.releaseVersion); 
     // this.total_services.push(value.creationDate); 
     ServersComponent.podState = JSON.stringify(value.state); 
     ServersComponent.podServiceName = JSON.stringify(value.serviceName); 
     ServersComponent.podDomainName = JSON.stringify(value.domainName); 
     ServersComponent.podReleaseVersion = JSON.stringify(value.releaseVersion); 
     ServersComponent.podCreationDate = JSON.stringify(value.creationDate); 
     ServersComponent.podBIServiceURL = JSON.stringify(value.BI_SERVICE_URL); 
     ServersComponent.podeditionDisplayName = JSON.stringify(value.editionDisplayName); 
     console.log('Pod State check' + ServersComponent.podState); 
     this.services.push(new ServerProperties(JSON.stringify(value.serviceName), JSON.stringify(value.state), 
      JSON.stringify(value.domainName), JSON.stringify(value.releaseVersion), 
      JSON.stringify(value.creationDate), JSON.stringify(value.BI_SERVICE_URL), JSON.stringify(value.editionDisplayName))); 
    }); 
    } 

    ngOnInit() { 
    } 

    onCreateServer(){ 
    this.serverCreationStatus = 'Server Instance Created! ' + this.serverName; 
    //Trying to get the JSON Response 
    return this.http.get('http://127.0.0.1:5000/PodChecker').subscribe((response: Response) => { 
     console.log(response); 
     const data = response.json(); 
     console.log(data); 
     this.Service_Instances = data; 
     this.dict = data; 
     console.log('Data[services]' + JSON.stringify(data['services'])); 
     console.log(this.dict); 
     this.Service_Instances = this.dict['services']; 
     //this.setValues(this.dict['services']); 
     console.log('Must Check' + JSON.stringify(this.setValues(this.dict['services']))); 
     console.log('This Services Output:' + JSON.stringify(this.services)); 
     }, 

     (error) => console.log(error) 
    ); 
    } 

    onUpdateServerName(event: Event){ 
    this.serverName = (<HTMLInputElement>event.target).value; 
    } 

} 

답변

1
jQuery.each(array, function(key, value) { 

jQuery.each(array, (key, value)=> { 

해야한다 :

내 코드입니다 : How to access the correct `this` context inside a callback?

+0

여기 문제는 push/set ServerProperties 개체를 this.services 정의 할 수 없습니다 정의되지 않은 내가 무엇입니까! –

+0

@RaghavendraSS 전체 ServersComponent 구성 요소를 업로드 할 수 있습니까? – echonax

+0

그래, 기본적으로 중첩 된 json 객체를 얻었고, 그때부터 vaules를 추출해 내고, 그 다음에 ServerProperties 생성자로 전달했지만 this.services 푸시 (new ServerProperties())를 수행 할 수 없다. –

관련 문제