【发布时间】:2014-07-31 05:19:52
【问题描述】:
我正在尝试使用 $resource 将数据绑定到 angular-ui ng-grid 但有些东西不起作用 - 选项正在应用于网格并且指令正在工作,因为我可以看到列标题但是没有数据
服务/工厂
angular.module('app').factory('mvVegetableService',function($resource){
var myVegetableResource = $resource('/api/vegetable/:_id', {_id:"@id"}, {
update: {method:'PUT', isArray: false}
});
return myVegetableResource;
});
控制器
angular.module('app').controller('mvVegetableCtrl', function($scope, mvVegetableService){
$scope.vegetableService = mvVegetableService.query();
$scope.dataVegetables = {};
$scope.vegetableGridOptions = {
data: $scope.dataVegetables,
columnDefs:[{
field: "colorCode",
displayName: "Color Code"
}]
};
$scope.vegetableService.$promise.then(function(vegetables){
$scope.dataVegetables = vegetables;
console.log(vegetables);
// this outputs
// 0: Resource
// expanded
// $$hashKey: "00D"
// __v: 0
// _id: "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
// colorCode: "GREEN"
// 1: Resource
// $promise: Object
// expanded
// catch: function (callback) {
// finally: function (callback) {
// then: function (callback, errback, progressback) {
// __proto__: Object
// $resolved: true
// length: 2
// $resolved: true
// length: 2
// __proto__: Array[0]
});
});
在同一页面/控制器上,我可以对选择元素中的选项进行 ng-repeat 重复,并且效果很好。
【问题讨论】:
-
嗯...这一切的顺序都正确吗?为什么在 ng-grid 选项之后调用该服务? ... $scope.dataVegetables = {}; $scope.vegetableGridOptions = { data: $scope.dataVegetables, ... 看起来您将一个空对象作为数据传递,然后您正在填充它。
-
这似乎是我在其他帖子中看到此网格绑定的模式。只要创建网格时变量存在(甚至为空),我就可以确定它的顺序正确
-
你试过没有 $promise 吗?或者更确切地说,返回的 $promise 对象的内容是什么?
-
我已经编辑了帖子以显示返回对象中内容的“扩展”版本 - 返回的 $promise 对象是“标准” - 我能够“绑定”到选择元素使用完全相同的代码和 ng-repeat
标签: angularjs ng-grid angularjs-resource