我是 Angularjs 的新手,以下 Spring Controller 从数据库获取对象,我想在 Angularjs Controller 中打印此对象属性,但我未定义
@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 Controller
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
也将是一个 promise ,所以你必须这样做:
ChartConfigService.findOne().then(function(obj) {
console.log(obj.type);
});
obj.type
未定义,因为 Promise 对象上不存在 type
。
关于javascript - AngularJS 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27769535/