Typescript Angular access to the parameter that should be delivered by the service

I have a problem accessing a variable that must be resolved by the service. If I use this variable directly from angular in html, there is no problem, but when I want to use it in a method, I become nothing. The service is working fine and this is a call to the http rest service. Here is the 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 is as follows:

  /// <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"]);
}
+4
source share
2 answers

I replaced the part in the controller constructor with $ prom.then, like this:

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

, . , , . 1- {} to query, 2- 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();
0

Source: https://habr.com/ru/post/1654915/


All Articles