Почему ошибка углового кода: невозможно прочитать свойство undefined? - PullRequest
0 голосов
/ 22 декабря 2018

Я получаю данные из веб-API, который возвращает объект JSON, он возвращается нормально, но когда я зацикливаюсь на этом, он выдает ошибку, которая не может прочитать свойство undefined 'CustomerName'.

    [System.Web.Http.HttpGet]
    public object GetBookings(string SearchByParam = "", byte WhichRecords 
      = 1)
    {
        List<ZahidCarWash.ViewModels.BookingArrivalCancellationViewModel> 
        lstBookingArrivalCancellation = 
      BookingsRepository.SelectCustomerBookingDetailsAndroid(SearchByParam, 
      WhichRecords);

        if (lstBookingArrivalCancellation.Count > 0)
        {
            return Json(new { ReturnStatusJSON = true, lstArrivalCancellation = lstBookingArrivalCancellation });
        }

        else
        {
            return Json(new { ReturnStatusJSON = false, lstArrivalCancellation = lstBookingArrivalCancellation });
        }
           }

Customer.service.ts

export class CustomerService {
    formData: Customer;
    Data: Customer[];

    constructor(private http: HttpClient) { }

    getData() {
        return
        this.http.get('http://localhost:13924/api/AndroidOperations/GetBookings')
            .subscribe(function(data: any) {
            this.Data = data['lstArrivalCancellation'] as Customer[];
            console.log(this.Data);
        });
    }

}

customer-list.component.ts

export class CustomersListComponent implements OnInit {

    constructor(public service: CustomerService) {
    }

    ngOnInit() {
        this.service.getData();
    }

}

.html

 <table class="table table-hover">
  <tr *ngFor="let d of service.Data | async"></tr>
   <td *ngIf="CustomerName">{{d.CustomerName}}</td>
   <td>Tr</td>
 </table>

Режим клиента:

   export class Customer {
     CustomerID: number ;
     CustomerName: string;
     ContactNo: string;
     VehicleRegNo: string;
     fk_VehicleMakeID: number;
     VehicleModel: number;

    }

Ответы [ 2 ]

0 голосов
/ 22 декабря 2018

Вы проверяете CustomerName в своем выражении * ngIf, где вы должны проверять d.CustomerName.'d' всегда будет там, так как вы перебираете коллекцию, в которой она находится. Но d.CustomerName не определено, это рельсы.

<div *ngIf="d.CustomerName">{{d.CustomerName}}</div

должен добиться цели.

0 голосов
/ 22 декабря 2018

Попробуйте изменить ngIf в HTML-код с

<td *ngIf="CustomerName">{{d.CustomerName}}</td>

на

<td *ngIf="d">{{d?.CustomerName}}</td>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...