I have different categories in the backend and I would like to retrieve them in a model format. Here is how my model is structured:
export class Category {
name: string;
id : string;
}
And this is how the data appears in the backend:
{
"name": "cars",
"id": "5a082ab8cb2b3f6b373e3042"
}
Below is the code for my service:
import { Category } from '../../core/models/category.model';
@Injectable()
export class CategoryService {
constructor(private _http: HttpClient) { }
fetchCategory()
{
return this._http.get<Category>(environment.apiPath+"categories");
}
}
Here is my component code:
import { Category } from '../../core/models/category.model';
@Component({
selector: 'app-barter-view',
templateUrl: './barter-view.component.html',
styleUrls: ['./barter-view.component.css'],
providers:[CategoryService]
})
export class BarterViewComponent implements OnInit {
allCategories :Category ;
constructor(private cact: CategoryService){}
ngOnInit() {
this.cact.fetchCategory() .subscribe(response=> {
console.log(response)
this.allCategories=response
}
);
}
Check out the console output here.