Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

How to use value of JSON object in TypeScript?

I was wondering how can I use string value of JSON object that I get from input.

export class NavComponent implements OnInit {
formVar: FormGroup;
items = [];

constructor(private api: ApiService, private fb: FormBuilder) {}

ngOnInit(): void {
  this.formVar = this.fb.group({
    search:''
  });
}

onSubmit(){
  this.api.search(this.formVar.value).subscribe(
    data => {
      this.items = data;
      console.log(this.formVar.value);
    }
  )
 }
}

The output for fromVar.value is this:

{search: "dfsdf"}
search: "dfsdf"
__proto__: Object

but I need only string value inside this object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I figured it out:

this.formVar.controls['search'].value

instead of:

this.formVar.value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...