2017-12-07 7 views
0

Error image
json 형식으로 개체에 액세스 할 수 있지만 모든 속성과 값을 볼 수는 있지만 수행 할 수는 없습니다. 개별 속성 값에 액세스하십시오.json 형식의 개체를 읽을 수 있지만 Angular2의 속성을 읽을 수 없음

전적으로 도움을 주셔서 감사합니다. 코드 아래

example.html 

<label>Name:</label> 

<input type="text" [(ngModel)]="yourName" placeholder="Enter a name here"> 
<input type="number" [(ngModel)]="age" placeholder="Enter age here"> 
<br> 
<button (click)="send({ name : yourName, age : age})">send</button> 

<h1 [hidden]="!yourName">Hello {{yourName}}!</h1> 
{{myobj | json}} 
{{myobj.name}} 




example.ts 

import {Component} from 'angular2/core'; 

@Component({ 
selector: 'hello-world', 
templateUrl: 'src/hello_world.html' 
}) 
export class HelloWorld { 
yourName: string = ''; 
myname : string; 
myage : number; 
send : any (obj){ 
    console.log(obj); 
    this.myobj = obj; 
} 

}

+0

에 대한 링크가 어디'myobj'이 정의인가? – cyberpirate92

+0

구문이 맞습니까? 나는 그것이 이어야한다고 생각한다. send : any = (obj) => { console.log (obj); this.myobj = obj; }' – cyberpirate92

+0

귀하의 제안을 모두 시도했지만 여전히 속성 값을 표시 할 수 없습니다. 그러나 나는 그것을 조작 할 수있다. – kkom

답변

0

이 나를 위해 작동합니다. 방금 구성 요소에 myobj:any을 정의하고 send 메서드 서명을 수정했습니다.

hello_world.html

<label>Name:</label> 
    <input type="text" [(ngModel)]="yourName" placeholder="Enter a name here"> 
    <input type="number" [(ngModel)]="age" placeholder="Enter age here"> 
    <br> 
    <button (click)="send({ name : yourName, age : age})">send</button> 

    <h1 [hidden]="!yourName">Hello {{yourName}}!</h1> 
    {{myobj | json}} 
    {{myobj.name}} 

helloworld.ts 여기

import {Component} from '@angular/core' 

@Component({ 
selector: 'hello-world', 
templateUrl: 'src/hello_world.html' 
}) 
export class HelloWorld { 
yourName: string = ''; 
myname : string; 
myage : number; 
myobj:any; 

constructor(){} 

public send(obj:any):void{ 
    console.log(obj); 
    this.myobj = obj; 
} 


} 

는 작업 plunker

https://plnkr.co/edit/5eNU8ermOX90UThetAJX?p=preview

+0

해결되었습니다. 시간 내 주셔서 감사합니다 !! – kkom

+0

@kkom 당신은 환영합니다 .. 당신이 도움이된다면 upvote 바랍니다 수 있습니다. – Niladri