英文:
Not able to show all the data from database to the angular project
问题
I'm developing one project and in that I've used Angular for frontend, Asp.net for API, and SQL-SERVER for Database. I'm wanted to show all the data from backend to the front API but not able to do show. I've mentioned the code below, kindly help me.
<table>
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>State</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let addr of service.AddressDetailsList">
<td>{{addr.AddressID}}</td>
<td>{{addr.Country}}</td>
<td>{{addr.State}}</td>
<td>{{addr.City}}</td>
</tr>
</tbody>
</table>
import { Component, OnInit } from '@angular/core';
import { FormDetailsService } from 'src/app/Services/form-details.service';
@Component({
selector: 'app-home-coming',
templateUrl: './home-coming.component.html',
styleUrls: ['./home-coming.component.css']
})
export class HomeComingComponent implements OnInit {
constructor(public service: FormDetailsService) { }
ngOnInit(): void {
this.service.getAddressDetailsnew().subscribe(data => {
this.service.AddressDetailsList = data;
})
}
}
readonly baseurl = "http://localhost:5157/api/RegistrationFormDetails/";
getAllState(): Observable<RegistrationFormDetails[]> {
return this.http.get<RegistrationFormDetails[]>(this.baseurl);
}
AddressDetailsList: AddressDetails[] = [];
getAddressDetailsnew(): Observable<AddressDetails[]> {
return this.http.get<AddressDetails[]>(this.baseurl + "Address")
}
[HttpGet("Address")]
public async Task<ActionResult<IEnumerable<Address>>> GetAddressDetails()
{
return await _context.Addresses.ToListAsync();
}
I've attached the code above. I've mentioned all the details there, while running that code, I'm not able to see the data, only the header I'm able to see in the browser.
英文:
I'm developing one project and in that i've used Angular for frontend, Asp.net for API and SQL-SERVER For Database. I'm wanted to show all the data from backend to the front api but not able to do show. I'v mentioned the code below, kindly help me.
<table>
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>State</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let addr of service.AddressDetailsList">
<td>{{addr.AddressID}}</td>
<td>{{addr.Country}}</td>
<td>{{addr.State}}</td>
<td>{{addr.City}}</td>
</tr>
</tbody>
</table>
import { Component, OnInit } from '@angular/core';
import { FormDetailsService } from 'src/app/Services/form-details.service';
@Component({
selector: 'app-home-coming',
templateUrl: './home-coming.component.html',
styleUrls: ['./home-coming.component.css']
})
export class HomeComingComponent implements OnInit {
constructor(public service:FormDetailsService) { }
ngOnInit(): void {
this.service.getAddressDetailsnew().subscribe(data=>{
this.service.AddressDetailsList=data;
})
}
}
readonly baseurl="http://localhost:5157/api/RegistrationFormDetails/";
getAllState():Observable<RegistrationFormDetails[]>{
return this.http.get<RegistrationFormDetails[]>(this.baseurl);
}
AddressDetailsList:AddressDetails[]=[];
getAddressDetailsnew():Observable<AddressDetails[]>{
return this.http.get<AddressDetails[]>(this.baseurl+"Address")
}
[HttpGet("Address")]
public async Task<ActionResult<IEnumerable<Address>>> GetAddressDetails()
{
return await _context.Addresses.ToListAsync();
}
I've attached the code above. I've mentioned all the details there, while runing that code i'm not able to see the data only the header i'm able to see in the brower.
答案1
得分: 1
你需要在你的组件中本地初始化一个变量,并使用它来填充模板,不涉及到服务。
你的组件:
@Component({
selector: 'app-home-coming',
templateUrl: './home-coming.component.html',
styleUrls: ['./home-coming.component.css']
})
export class HomeComingComponent implements OnInit {
listData: AddressDetails;
constructor(public service: FormDetailsService) { }
ngOnInit(): void {
this.service.getAddressDetailsnew().subscribe((data: AddressDetails[]) => {
this.listData = data;
})
}
}
模板:
<tbody>
<tr *ngFor="let addr of listData">
<td>{{addr.AddressID}}</td>
<td>{{addr.Country}}</td>
<td>{{addr.State}}</td>
<td>{{addr.City}}</td>
</tr>
</tbody>
英文:
You need to initialise a variable locally in your component and use that to fill the template, not involving the service.
Your component:
@Component({
selector: 'app-home-coming',
templateUrl: './home-coming.component.html',
styleUrls: ['./home-coming.component.css']
})
export class HomeComingComponent implements OnInit {
listData: AddressDetails[];
constructor(public service:FormDetailsService) { }
ngOnInit(): void {
this.service.getAddressDetailsnew().subscribe((data: AddressDetails[]) => {
this.listData = data;
})
}
}
The template
<tbody>
<tr *ngFor="let addr of listData">
<td>{{addr.AddressID}}</td>
<td>{{addr.Country}}</td>
<td>{{addr.State}}</td>
<td>{{addr.City}}</td>
</tr>
</tbody>
答案2
得分: 0
一切都很好,在这里,问题出在命名约定上,Angular 14采用了这种命名约定,如“userName”,如果输入值为“UserName”,也是如此。所以改变命名约定对我有用。
Francesco 提供的解决方案完全正确,按照他的代码进行更改后,我成功获得了解决方案。
英文:
Everything was good in this, the problem lies in the naming convention, where Angular 14 is taking naming convention like this "userName" if the written value is "UserName" is this also. So changing the naming convention worked for me.
Above solution provided by Francesco was totally correct and after making change by following his code, I was able to get the solution.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论