Typescript Angular的访问参数,应该由service交付
Typescript Angular accessing parameter that should be delivered by service
我访问变量时遇到问题,应该由服务解决。如果我在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();
相关文章:
- 测试Angular Service解决错误回调中的promise
- 在同一个服务工作者中处理service-worker.js有任何影响吗
- 是否存在Javascript Liferay Service库的文档?如何处理错误情况
- 如何在单元测试中将存储注入Ember.Service
- 在线数据库WebApp/Service
- 在Service worker中使用setInterval
- 控制器无法访问Angular Service变量
- jquery在通过谷歌标签管理器交付时点击不工作
- consume salesforce chatter rest service from pure javascript
- 希望创建一个显示日期+5天和日期+10天之间交付的javascript
- Angular Service数据未在$scope中更新
- windows 8 wcf web service (localhost/intranet)
- 在Angular Service/Controller中实现安全$apply的最佳方法
- 将通过Ajax(和Web Service方法)获得的数组转换为javascript数组
- WCF Restful Service (Windows Auth) with JSONP
- fire js函数,当所有脚本都通过CDN交付时
- AngularJS service with Java (REST)?
- 在Rails中准确测试JavaScript分析交付系统
- 优化谷歌推荐的CSS交付
- Typescript Angular的访问参数,应该由service交付