$promise 在 'then' 函数上保持承诺
$promise stays promised on 'then' function
我正在使用 angular 的 $resource
来执行 GET 操作。我是这样做的:
var query = {customerid: $stateParams.customerid}
$resource('api/reports/running_count').get(query).$promise.then(function(value) {
$scope.runningInstance = value;
});
我也这样试过:
$resource('api/reports/running_count').get(query, function(value) {
$scope.runningInstance = value;
});
请求returns一个号码。我用 chrome 的开发人员工具检查了响应。请求确实发送如下:
<base-url>/api/reports/running_count?customerid=<id>
响应 returns 一个数字,同样符合预期。
但是当我在回调函数中放置一个断点时,value
又是一个承诺而不是一个数字。不确定我做错了什么。
来自 angular 示例 https://docs.angularjs.org/api/ngResource/service/$resource:
var User = $resource('/user/:userId', {userId:'@id'});
User.get({userId:123}, function(user, getResponseHeaders){
user.abc = true;
user.$save(function(user, putResponseHeaders) {
//user => saved user object
//putResponseHeaders => $http header getter
});
});
把你的价值观放在那里。
据我所知,如果您想使用 query value
获取数据,那么服务器会将 query value
作为字符串获取。当您从搜索框发送查询时,它很有用。例如,想按名称或其他方式搜索产品,则可以使用 like。
在役:
var query = {productname: '123'}
getProducts: function(query) {
var getProducts = $resource('/api/products', {}, {
'get': {method: 'GET', isArray: true}
});
return getProducts.get(query);
}
并且服务器端将接收为 req.query.productname
,因此产品名称为 string
,因此如果需要,您需要将其转换为 number
作为数字。
但是如果你想通过 id
找到产品你应该发送 id
作为参数而不是查询 完全匹配 也可以作为字符串发送.
例如在服务中:
getProductById: function(id) {
var getProductById = $resource('/api/products/:id', {id: '@id'}, {
'get': {method: 'GET'}
});
return getProductById.get({id: id});
}
所以服务器端将收到 req.params.id
当响应是原始类型时,$resource
服务不工作。它使用 angular.copy
将数据复制到 $resource
对象,而 angular.copy
不会将原始数据复制到对象。它只能对其他对象的属性进行深拷贝。
来自文档:1
angular.copy
Creates a deep copy of source
, which should be an object or an array.
If a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted and then all elements/properties from the source are copied to it.
If source
is not an object or array (inc. null
and undefined
), source
is returned.
在您的情况下,源不是对象,您得到的只是 $promise
属性,$resource
服务附加到资源对象。
我正在使用 angular 的 $resource
来执行 GET 操作。我是这样做的:
var query = {customerid: $stateParams.customerid}
$resource('api/reports/running_count').get(query).$promise.then(function(value) {
$scope.runningInstance = value;
});
我也这样试过:
$resource('api/reports/running_count').get(query, function(value) {
$scope.runningInstance = value;
});
请求returns一个号码。我用 chrome 的开发人员工具检查了响应。请求确实发送如下:
<base-url>/api/reports/running_count?customerid=<id>
响应 returns 一个数字,同样符合预期。
但是当我在回调函数中放置一个断点时,value
又是一个承诺而不是一个数字。不确定我做错了什么。
来自 angular 示例 https://docs.angularjs.org/api/ngResource/service/$resource:
var User = $resource('/user/:userId', {userId:'@id'});
User.get({userId:123}, function(user, getResponseHeaders){
user.abc = true;
user.$save(function(user, putResponseHeaders) {
//user => saved user object
//putResponseHeaders => $http header getter
});
});
把你的价值观放在那里。
据我所知,如果您想使用 query value
获取数据,那么服务器会将 query value
作为字符串获取。当您从搜索框发送查询时,它很有用。例如,想按名称或其他方式搜索产品,则可以使用 like。
在役:
var query = {productname: '123'}
getProducts: function(query) {
var getProducts = $resource('/api/products', {}, {
'get': {method: 'GET', isArray: true}
});
return getProducts.get(query);
}
并且服务器端将接收为 req.query.productname
,因此产品名称为 string
,因此如果需要,您需要将其转换为 number
作为数字。
但是如果你想通过 id
找到产品你应该发送 id
作为参数而不是查询 完全匹配 也可以作为字符串发送.
例如在服务中:
getProductById: function(id) {
var getProductById = $resource('/api/products/:id', {id: '@id'}, {
'get': {method: 'GET'}
});
return getProductById.get({id: id});
}
所以服务器端将收到 req.params.id
当响应是原始类型时,$resource
服务不工作。它使用 angular.copy
将数据复制到 $resource
对象,而 angular.copy
不会将原始数据复制到对象。它只能对其他对象的属性进行深拷贝。
来自文档:1
angular.copy
Creates a deep copy of
source
, which should be an object or an array.
If a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted and then all elements/properties from the source are copied to it.
If
source
is not an object or array (inc.null
andundefined
),source
is returned.
在您的情况下,源不是对象,您得到的只是 $promise
属性,$resource
服务附加到资源对象。