我从api中提取数据并尝试绑定到变量,我调试了从API获取数据的代码但是它没有绑定到范围对象这里是我的代码
服务
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");?
这是我要调用服务和使用
的Controller代码module application.common {
angular.module("common.service", ["ngresource"])
export class DataAccessService {
static $inject = ["$resource"]; /// To avoid minificaton errors i.e prameters thgar are use to be defines and should be in order.
constructor(private $resource: ng.resource.IResourceService,private $http:ng.IHttpService) {
}
getProductResource() {
return this.$resource("http://localhost:50000/api/values/GetProducts", { 'query': { method: 'GET', isArray: false } }).get().$promise.then(function (response) {
var obj = new product();
//debugger;
obj.title = response.title;
obj.showImage = response.showImage;
obj.products = response.objDetails;
return obj;
});
}
}
angular.module("common.service",['ngResource']).service("dataAccessService", DataAccessService);
}
实体:
class ProductListCtrl1 {
obj: IProduct;
static $inject = ["dataAccessService"];
constructor(private dataAccessService: application.common.DataAccessService) {
this.obj = new product();
dataAccessService.getProductResource().then(function (response) {
alert('1');
debugger;
this.obj = response; // I'm getting response here with all properties
});
alert('2 ' + this.obj.title); // This alert is firing first rather than alert 1
this.obj.title = "Product List Type Script11"; // This is getting binded what ever properties I'm binding here are loaded
}
}
var app1 = angular.module("productManagement1", ['ngRoute', 'ngResource','common.service']);
app1.controller("ProductListCtrl1", ProductListCtrl1);
根据评论我附上我的回复对象
interface IProduct {
title: string;
showImage: boolean;
products: productDetails[];
}
class product implements IProduct {
title: string;
showImage: boolean;
products: productDetails[];
}