3

I am a new angular developer but I don't know where I mistake. I find a lot of sources but ı did not adapt my codes. My codes do not work or make get from web client Could you help me with this issue?

My example JSON Data :

{ "id": "123aCaA-9852", "name": "A bank", "address": "adres", "contactName": "Example name", "contactSurname": "Exmple", "contactPhone": "12312312312", "status": false, "city": { "id": 40, "name": "Turkey", "code": 34 }, My Example ts:

export class Kafein {
    name:string;
    address:string;
}

My example kafein.component.ts

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Kafein } from './kafein';
import { Observable } from 'rxjs/internal/Observable';
@Component({
  selector: 'app-kafein',
  templateUrl: './kafein.component.html',
  styleUrls: ['./kafein.component.css']
})
export class KafeinComponent implements OnInit {
  title = "Kafein";
  //kafein:Observable<Kafein[]>;
  kafein:Kafein[];
  constructor(private http: HttpClient) { }

  ngOnInit() {
    this.getProducts();
  }
  getProducts(){

   //this.kafein = this.http.get<Kafein[]>('http:example');
   this.http.get<Kafein[]>('http:example').subscribe(res=>{this.kafein=res});

  } 

}

My example component.html

<tr *ngFor='let kafein of Kafein'>
        <th scope="row">1</th>
        <td>{{kafein.name}}</td>
        <td>{{kafein.adress}}</td>
</tr>
4
  • what is error in console ? Commented Aug 2, 2018 at 12:10
  • return empty data on my screen Commented Aug 2, 2018 at 12:29
  • @EmreSert have you tried this : this.kafein=res as Kafein[] Commented Aug 2, 2018 at 12:59
  • I solved the problem.Thanks a lot. Commented Aug 2, 2018 at 13:33

5 Answers 5

2

You should name your variables bit better way:

let kaf of kafein // <---change the K in Kafein to k(lowercase)

and use kaf as variable in the template.

<tr *ngFor='let kaf of kafein'>
     <th scope="row">1</th>
     <td>{{kaf.name}}</td>
     <td>{{kaf.adress}}</td>
</tr>
Sign up to request clarification or add additional context in comments.

Comments

0

You had already got api data into your variable. Just change spelling from 'let kafein of Kafein' to 'let kaf of kafein''

1 Comment

did you got data in console? Please use console when you bind data from api. and in your above json you got more than two object. So please use it without your class. just bind it to one variable and use it directly in your view.
0

Make a service.ts file and make a function like this

getUsers(){
return  this.httpClient.get("http:example");
}

And import this service.ts in your component and then call your service function like this

getUsers(){
this.apiService.getUsers().subscribe((data:  Array<object>) => {
    this.kafein  =  data;
    console.log(data);
});

If you want further guidance, follow this Link

Comments

0

ngFor is for looping through an Array, but your data contains an object. I am going to guess the following code would work:

<tr>
        <th>1</th>
        <td>{{kafein['name']}}</td>
        <td>{{kafein['adress']}}</td>
</tr>

But that is after making sure you are getting data in the first place. For that check the answer of Ahsan Alam Siddiqui

Comments

0

According to my researching , I need to make Authorization process with token for my api because of being user data.By the way, i took my api from bank server and i forgot to say you.The problem solved with the following code.A lot of thanks for lowercase kafein instead of Kafein :)

httpOptions = {
    headers: new HttpHeaders({
      'Authorization':'bearer exampletoken',// TOKEN 
      'Access-Control-Allow-Origin':'*',
      'Access-Control-Allow-Methods':'*',
      'Access-Control-Allow-Headers':'*'
    }),
    withCredentials: false
  };

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.