我正在使用Typescript处理AngularJS 2应用。我遇到一个问题,我无法使用数据绑定访问HTML页面中的Typescript类中定义的属性。我在下面列出了相关文件。如果我错过了什么,请告诉我。我现在看这个问题超过2天了,目前我的想法不合时宜。
打字稿类。
export class ApproveBooking {
constructor(
public name: string,
public form: DynamoForm,
public cabBooking: CabBooking) {}
}
export class DynamoForm {
constructor(
public formFields: DynamoFormField[]) { }
}
export class DynamoFormField {
constructor(
public fieldName: string,
public fieldId: string,
public fieldLabel: string,
public fieldType: string,
public fieldValue: string,
public required: boolean) { }
}
export class CabBooking {
constructor(
public id: number,
public bookingId: string,
public status: string,
public notes: string,
public user: boolean,
public travelDateString: string,
public travelTimeString: string,
public pickupLocation: string,
public dropLocation: string,
public approver: string,
public approverAction: string,
public approverComments: string,
public approvedDate: string,
public cabDriver: string,
public cabDriverAssignedDate: string,
public createdDate: string,
public modifiedDate: string) { }
}
从服务器获取JSON数据的服务,并将其分配给' ApproveBooking'类。
getSingleCabBookingForApproval(bookingId: string) {
var accessToken = localStorage.getItem('cabservice_access_token');
var approveUrl = this._appService.getAppUrl() + "/dynamo/api/cabservice/bookings/approve/" + bookingId + "?access_token=" + accessToken;
return this._http.get(approveUrl)
.map(res => {
return <ApproveBooking> res.json();
})
.catch(this.handleError);
}
从服务器收到的JSON值如下:
{
"name": "Name set in backend...",
"form": {
"formFields": [
{
"fieldName": "approverComments",
"fieldId": "approverComments",
"fieldLabel": "Approver Comments",
"fieldType": "textarea",
"fieldValue": "",
"required": false
},
{
"fieldName": "approverAction",
"fieldId": "approverAction",
"fieldLabel": "Approve Cab Request ",
"fieldType": "radio",
"fieldValue": [
{
"label": "Approve",
"name": "approve",
"selected": false
},
{
"label": "Reject",
"name": "reject",
"selected": false
}
],
"required": false
},
{
"fieldName": "approvedDate",
"fieldId": "approvedDate",
"fieldLabel": "Approved Date",
"fieldType": "datetime-local",
"fieldValue": "",
"required": true
}
]
},
"cabBooking": {
"id": 19,
"bookingId": "BId_1453269360424",
"status": "New",
"notes": null,
"user": "karthik",
"travelDate": 1453228200000,
"travelDateString": null,
"travelTimeString": null,
"pickupLocation": "velachery",
"dropLocation": "chennai one",
"approver": "sivaram",
"approverAction": null,
"approverComments": null,
"approvedDate": null,
"cabDriver": null,
"cabDriverAssignedDate": null,
"createdDate": 1453269360000,
"modifiedDate": 1453269360000
}
}
使用服务方法填充其中一个属性的AngularJS组件。
@Component({
templateUrl: 'app/approver/approver_approvebooking.html'
})
export class ApproveCabBookingComponent implements OnInit {
private errorMessage: string;
private approveBooking: ApproveBooking;
constructor(
private _logger: Logger,
private _router: Router,
private _routeParams: RouteParams,
private _cabBookingService: CabBookingService) { }
ngOnInit() {
let bookingId = this._routeParams.get('bookingId');
this._logger.log("bookingId = " + bookingId);
this.approveBooking = this._cabBookingService.getSingleCabBookingForApproval(bookingId)
.subscribe(
approveBooking => {
this.approveBooking = approveBooking;
this._logger.log("this.approveBooking => " + JSON.stringify(this.approveBooking));
},
err => {
this._logger.log("Error =>" + err.status + "," + JSON.stringify(err));
},
() => console.log('Approve Cab Booking Entity Fetched!');
);
}
}
组件的HTML模板:
<div class="col-md-12 title-panel">
<h3>Booking Status</h3>
</div>
<div class="col-md-12 content-panel">
<div class="row">
<div class="col-md-12">
<h4 class="label-heads">Booking ID</h4>
<div class="form-value">
<span>{{approveBooking.cabBooking.bookingId}}</span>
</div>
</div>
</div>
</div>
我遇到的问题是当我尝试在HTML视图中使用approveBooking的内部属性时,它会在下面引发错误。
TypeError: Cannot read property 'bookingId' of undefined in [{{approveBooking.cabBooking.bookingId}} in ApproveCabBookingComponent@8:10]
仅供参考,当我使用平面JSON数据和平面的Typescript类时,事情正在按预期工作。
答案 0 :(得分:0)
经过一些研究,我发现问题是由于组件模板(HTML)甚至在JSON数据可用于相应组件之前就已呈现。
以下帖子有助于确定原因。 Angular2: How to load data before rendering the component?
通过遵循那里提供的解决方案,我更新了AngularJS组件。它在下面提供。
@Component({
templateUrl: 'app/approver/approver_approvebooking.html'
})
export class ApproveCabBookingComponent implements OnInit {
private errorMessage: string;
private approveBooking: ApproveBooking;
private isDataAvailable: boolean;
constructor(
private _logger: Logger,
private _router: Router,
private _routeParams: RouteParams,
private _cabBookingService: CabBookingService) {
this.isDataAvailable = false;
}
ngOnInit() {
let bookingId = this._routeParams.get('bookingId');
this._logger.log("bookingId inside ngInit = " + bookingId);
this.approveBooking = this._cabBookingService.getSingleCabBookingForApproval(bookingId)
.subscribe(
approveBooking => {
this.approveBooking = approveBooking;
this.isDataAvailable = true;
this._logger.log("this.approveBooking => " + JSON.stringify(this.approveBooking));
},
err => {
this._logger.log("Error while accessing approveBooking...error, JSONed = " + err.status + "," + JSON.stringify(err));
},
() => console.log('Approve Cab Booking Entity Fetched!');
);
}
}
相应的视图已更新为使用&#39; isDataAvailable&#39;在组件中设置的属性。
<div *ngIf="isDataAvailable">
<div class="col-md-12 title-panel">
<h3>Manage Booking </h3>
</div>
<div class="col-md-12 content-panel">
<form (ngSubmit)="onSubmit()">
<div class="row">
<div class="col-md-12">
<h4 class="label-heads">Booking ID</h4>
<div class="form-value">
<span>{{approveBooking.cabBooking.bookingId}}</span>
</div>
</div>
</div>
</form>
</div>
</div>