AngularJS 对象
AngularJS object
我是 Angularjs 的新手,以下 Spring 控制器从数据库中获取对象 我想在 angularjs 控制器中打印此对象属性,但我得到未定义
@RequestMapping(value = "/rest/getById",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@RolesAllowed(AuthoritiesConstants.ADMIN)
public ChartConfigs findOne(Integer id) {
return chartConfigService.getOne(1);
}
这是Angularjs服务
myappApp.factory('ChartConfigService', function ($http) {
return {
findOne: function() {
var promise = $http.get('app/rest/chartConfigs/getById').then(function (response) {
return response.data;
});
return promise;
}
}
});
这是Angularjs控制器
myappApp.controller('ChartConfigController', function ($scope, ChartConfigService) {
$scope.message = ChartConfigService.findOne();
var obj=ChartConfigService.findOne();
console.log(obj.type);
});
chartconfig 域
包 com.innvo.domain;
@Entity
@Table(name = "T_CHART_CONFIGS")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ChartConfigs {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Integer id;
@Column(name = "category")
private String category;
@Column(name = "type")
private String type;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
在你的情况下 obj
也是一个承诺,所以你必须这样做:
ChartConfigService.findOne().then(function(obj) {
console.log(obj.type);
});
obj.type
未定义,因为 type
在 promise 对象上不存在。
我是 Angularjs 的新手,以下 Spring 控制器从数据库中获取对象 我想在 angularjs 控制器中打印此对象属性,但我得到未定义
@RequestMapping(value = "/rest/getById",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@RolesAllowed(AuthoritiesConstants.ADMIN)
public ChartConfigs findOne(Integer id) {
return chartConfigService.getOne(1);
}
这是Angularjs服务
myappApp.factory('ChartConfigService', function ($http) {
return {
findOne: function() {
var promise = $http.get('app/rest/chartConfigs/getById').then(function (response) {
return response.data;
});
return promise;
}
}
});
这是Angularjs控制器
myappApp.controller('ChartConfigController', function ($scope, ChartConfigService) {
$scope.message = ChartConfigService.findOne();
var obj=ChartConfigService.findOne();
console.log(obj.type);
});
chartconfig 域
包 com.innvo.domain;
@Entity
@Table(name = "T_CHART_CONFIGS")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ChartConfigs {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Integer id;
@Column(name = "category")
private String category;
@Column(name = "type")
private String type;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
在你的情况下 obj
也是一个承诺,所以你必须这样做:
ChartConfigService.findOne().then(function(obj) {
console.log(obj.type);
});
obj.type
未定义,因为 type
在 promise 对象上不存在。