我第一次選擇Angular並且來自.NET背景感覺使用typescript更舒適。AngularJS Typescript控制器值不更新頁面
我遇到了一個問題,當控制器使用從服務接收的對象填充控制器值時,似乎無法在頁面上更新值。我可能只是在做一個noob錯誤。
任何人都可以指出我做錯了什麼。
所以我的打字稿是
module ConnectAdmin.Interfaces {
export interface ITemplate {
templateId: number;
name: string;
description: string;
}
}
module ConnectAdmin.Interfaces {
export interface ITemplateCollection {
total: number;
set: number;
skipped: number;
collection: Array<ITemplate>;
}
}
module ConnectAdmin.Controllers {
import TemplateCollection = Interfaces.ITemplateCollection;
export class TemplateIndexController {
static $inject = ["ConnectAdmin.Services.TemplateService"];
constructor(templateService: ConnectAdmin.Services.TemplateService) {
this.defaultTemplates = { skipped: 0, set: 0, total: 0, collection: [] };
this.templates = this.defaultTemplates;
this.processing = true;
this.store = this;
templateService.index(this.take, this.skip, this.successCallback, this.errorCallback);
this.processing = false;
}
successCallback(data: TemplateCollection) {
this.templates = { skipped: 0, set: 0, total: 0, collection: [] }
this.templates = data;
alert(this.templates.collection.length);
}
errorCallback(response: any) {
this.templates = this.defaultTemplates;
alert(response.status);
this.message = "An Error Occurred Contacting the API";
}
processing: boolean;
store = this;
defaultTemplates: TemplateCollection;
templates: TemplateCollection;
take = 20;
skip = 0;
message: string;
}
angular.module("ConnectAdmin").controller("ConnectAdmin.Controllers.TemplateIndexController", TemplateIndexController);
}
module ConnectAdmin.Services {
import TemplateCollection = Interfaces.ITemplateCollection;
import TemplateIndexController = Controllers.TemplateIndexController;
export class TemplateService {
constructor($http: ng.IHttpService) {
this.http = $http;
}
http: ng.IHttpService;
index(take: number, skip: number, successCallback: Function, errorCallback: Function) {
const req = {
method: "GET",
url: "https://localhost:44336/api/Templates?take=" + take + "&skip=" + skip,
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
};
this.http(req).then(response => {
successCallback(response.data);
},
response => {
errorCallback(response);
});
//return { total: 1, skipped: 0, set: 1, collection: [{ templateId: 1, name: "Template 1", description: "" }] };
}
}
angular.module("ConnectAdmin").service("ConnectAdmin.Services.TemplateService", TemplateService);
}
與我的HTML之中:
<div id="template-index">
<div class="row">
<div class="col-sm-12">
<div class="info-box">
<div class="info-box-title">
Templates
</div>
<div class="info-box-content" ng-controller="ConnectAdmin.Controllers.TemplateIndexController as templateCtrl">
<table class="table table-striped table-responsive">
<thead>
<tr>
<td>#</td>
<td>Name</td>
<td>Description</td>
</tr>
</thead>
<tbody>
<tr class="tr-select" ng-click="templateCtrl.openTemplate(template.templateId)" ng-repeat="template in templateCtrl.templates.collection">
<td>{{template.templateId}}</td>
<td>{{template.name}}</td>
<td>{{template.description}}</td>
</tr>
</tbody>
</table>
<div id="template-index-loader" class="loader" ng-show="templateCtrl.processing"></div>
<div class="info-box-footnote" ng-hide="templateCtrl.templates.collection.length">
Displaying {{templateCtrl.templates.skipped + 1}} to {{templateCtrl.templates.set + templateCtrl.templates.skipped}} of {{templateCtrl.templates.total}}
</div>
<div class="info-box-footnote" ng-show="templateCtrl.message.length">
{{templateCtrl.message}}
</div>
</div>
</div>
</div>
</div>
我不認爲它的HTTP調用因爲我沒有得到任何更新,如果我返回一個硬編碼目的。
成功回調中的警報給了我一個正確的值。
感謝