无法在Angular项目中显示来自数据库的所有数据。

huangapple go评论59阅读模式
英文:

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.

&lt;table&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;ID&lt;/th&gt;
            &lt;th&gt;Country&lt;/th&gt;
            &lt;th&gt;State&lt;/th&gt;
            &lt;th&gt;City&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr *ngFor=&quot;let addr of service.AddressDetailsList&quot;&gt;
            &lt;td&gt;{{addr.AddressID}}&lt;/td&gt;
            &lt;td&gt;{{addr.Country}}&lt;/td&gt;
            &lt;td&gt;{{addr.State}}&lt;/td&gt;
            &lt;td&gt;{{addr.City}}&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
import { Component, OnInit } from &#39;@angular/core&#39;;
import { FormDetailsService } from &#39;src/app/Services/form-details.service&#39;;

@Component({
  selector: &#39;app-home-coming&#39;,
  templateUrl: &#39;./home-coming.component.html&#39;,
  styleUrls: [&#39;./home-coming.component.css&#39;]
})
export class HomeComingComponent implements OnInit {

  constructor(public service:FormDetailsService) { }

  ngOnInit(): void {
    this.service.getAddressDetailsnew().subscribe(data=&gt;{
      this.service.AddressDetailsList=data;
    })
    
  }

}

readonly baseurl=&quot;http://localhost:5157/api/RegistrationFormDetails/&quot;;
  getAllState():Observable&lt;RegistrationFormDetails[]&gt;{
    return this.http.get&lt;RegistrationFormDetails[]&gt;(this.baseurl);
  }

  AddressDetailsList:AddressDetails[]=[];
 
  getAddressDetailsnew():Observable&lt;AddressDetails[]&gt;{
    return this.http.get&lt;AddressDetails[]&gt;(this.baseurl+&quot;Address&quot;)
  }
  
  
 [HttpGet(&quot;Address&quot;)]

        public async Task&lt;ActionResult&lt;IEnumerable&lt;Address&gt;&gt;&gt; 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: &#39;app-home-coming&#39;,
  templateUrl: &#39;./home-coming.component.html&#39;,
  styleUrls: [&#39;./home-coming.component.css&#39;]
})
export class HomeComingComponent implements OnInit {

  listData: AddressDetails[];  

  constructor(public service:FormDetailsService) { }

  ngOnInit(): void {
    this.service.getAddressDetailsnew().subscribe((data: AddressDetails[]) =&gt; {
      this.listData = data;
    })
  }
}

The template

&lt;tbody&gt;
    &lt;tr *ngFor=&quot;let addr of listData&quot;&gt;
        &lt;td&gt;{{addr.AddressID}}&lt;/td&gt;
        &lt;td&gt;{{addr.Country}}&lt;/td&gt;
        &lt;td&gt;{{addr.State}}&lt;/td&gt;
        &lt;td&gt;{{addr.City}}&lt;/td&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;

答案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.

huangapple
  • 本文由 发表于 2023年3月1日 16:26:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定