简体   繁体   English

Typescript应该由服务提供的角度访问参数

[英]Typescript Angular accessing parameter that should be delivered by service

I have problem when I access variable, that should be resolved by service. 我访问变量时遇到问题,应由服务解决。 If I use this variable directly with angular in html there is no problem, but when I want to use it in method I become nothing. 如果我在html中使用此变量直接使用angular是没有问题的,但是当我想在方法中使用它时,我就变成了什么。 Service is working fine and it is http rest service call. 服务很好,它是http休息服务电话。 Here's 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;
    };
}

And the Service looks like this: 服务看起来像这样:

  /// <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"]);
}

I replaced part in the constructor of controller with $promise.then construct like this: 我用$ promise.then结构替换了控制器构造函数中的part,如下所示:

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

Although you solved the problem in a correct way,I want to point out the real issue. 虽然你以正确的方式解决了这个问题,但我想指出真正的问题。 The real problem was you haven't pass query parameter in your query request which is 1st parameter. 真正的问题是你没有在查询请求中传递查询参数,这是第一个参数。 You should pass 1st parameter as {} to query function thereafter we can pass success & error callbacks respectively 2nd & 3rd. 您应该将第一个参数作为{}传递给query函数,然后我们可以分别传递成功和错误回调第2和第3个。

Code

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

Though another way is you can directly assign $resource promise object to desired variable so when promise get resolved API will unwrapped that promise and assign received data to this.response . 虽然另一种方法是你可以直接将$resource promise对象分配给所需的变量,这样当promise得到解决时,API将解包该promise,并将接收到的数据分配给this.response

this.response  = dataAccessService.getDataResource();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM