Typescript Angular的访问参数,应该由service交付

Typescript Angular accessing parameter that should be delivered by service

本文关键字:service 交付 Angular 访问 参数 Typescript      更新时间:2023-09-26

我访问变量时遇到问题,应该由服务解决。如果我在html中直接与angular一起使用这个变量是没有问题的,但是当我想在method中使用它时,我就什么都没有了。服务工作正常,它是http rest服务调用。控制器:

///<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代替了控制器构造函数中的一部分。然后像这样构造:

 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);
                }
            }
        );

虽然你解决问题的方法是正确的,但我想指出真正的问题。真正的问题是你没有在你的查询请求中传递查询参数,这是第一个参数。您应该将第一个参数作为{}传递给query函数,然后我们可以传递成功&;错误回调函数分别为2nd &3 .

var documentResource = dataAccessService.getDataResource();
documentResource.query({}, //1st parameter should pass as a blank
   (data: domain.IEntity) => {
     this.response = data;
   }
); 

虽然另一种方法是你可以直接分配$resource承诺对象到所需的变量,所以当承诺得到解决API将打开该承诺和分配接收到的数据给this.response

this.response  = dataAccessService.getDataResource();