我在访问变量时遇到问题,应该由服务解决。如果我在 html 中直接将此变量与 Angular 一起使用,则没有问题,但是当我想在方法中使用它时,我什么都不是。服务工作正常,它是 http rest 服务调用。 这是 Controller :
///<reference path='../domain/DocumentEntity.ts' />
///<reference path='../_app.ts'/>
module domain {
import DataAccessService = domain.DataAccessService;
import IDocument = domain.IDocument;
import DocumentEntity = domain.DocumentEntity;
import IEntity = domain.IEntity;
import Structure = domain.Structure;
interface IDocListController {
response: IEntity;
locations: IEntity;
structures: IStructure[];
}
export class DocController implements IDocListController {
title: string;
response: domain.IEntity;
locations: domain.IEntity;
structures: IStructure[];
static $inject = ["DataAccessService", "$scope"];
constructor(private dataAccessService: DataAccessService, $scope) {
this.title = "Document Listing";
//test
var documentResource = dataAccessService.getDataResource();
documentResource.query((data: domain.IEntity) => {
this.response = data;
});
$scope.vm = this;
console.log($scope.vm);
if (!this.response || !this.response.folders.length) {
console.log("NO RESPONSE RETURNING NOTHING");
return;
}
this.structures = this.createFolderStructure(this.response.folders, 4);
console.log(this.structures);
}
createFolderStructure(folders: IFolder[], depth: number): IStructure[] {
var structures: Structure[] = [];
for (var i = 0; i < folders.length; i++) {
let str: Structure = new Structure();
str.id = folders[i].id.toPrecision();
str.isFolder = true;
str.name = folders[i].name;
str.structures = this.createFolderStructure(folders, depth - 1);
structures.push(str);
}
console.log(structures);
return structures;
};
}
服务看起来像这样:
/// <reference path='../_app.ts' />
module domain {
import DocumentEntity = domain.DocumentEntity;
export interface IDataAccessService {
getDataResource(): ng.resource.IResourceClass<IEntityResource>;
}
export interface IEntityResource extends ng.resource.IResource<domain.Entity> {
}
export class DataAccessService implements IDataAccessService {
//minification protection
static $inject = ["$resource"]
constructor(private $resource: ng.resource.IResourceService) {
console.log("DataAccessService Constructor");
}
getDataResource(): ng.resource.IResourceClass<IEntityResource> {
console.log("REST CALL");
return this.$resource("http://localhost:8080/services/name/:searchId/documents/", {searchId: "12345678"}, {
'query': {
method: 'GET',
isArray: false
}
});
}
}
angular.module("common.services", ["ngResource"]);
}
最佳答案
我用 $promise 替换了 Controller 构造函数中的部分。然后像这样构造:
documentResource.query((data: domain.IEntity) => {
this.response = data;
}).$promise.then((data)=> {
if (this.response.folders) {
this.structures = [];
for (let i = 0; i < this.response.folders.length; i++) {
if(this.response.folders){
Array.prototype.push.apply(this.structures,this.createFolderStructure(this.response.folders, 4));
}
}
console.log(this.structures);
}
}
);
关于javascript - 应由服务传递的 Typescript Angular 访问参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39549049/