$resource中的数据未同步到从HTML调用
Data from $resource not synchronized to call from HTML
我能够在控制器中的vm.getProduct()中获得返回的数组的print。但这在HTML中不可用。
资源:
var DataProvider = function ($resource) {
return $resource('', {}, {
getProductTypeAhead: {
method: 'GET',
isArray: true,
url: '/productinterface?typeahead=:typeAhead',
params: {typeAhead: '@typeAhead'}
}
});
};
module.exports = ['$resource', DataProvider];
服务:
var DataService = function (DataProvider, $scope) {
this.getProductTypeAhead = function (typeAhead, callback) {
DataProvider.getProductTypeAhead({typeAhead: typeAhead},
function (data) {
callback(data);
}, function (data) {
// NOTIFY THE USER THAT THE REQUEST FAILED
callback(null);
});
};
};
module.exports = ['DataProvider', DataService];
控制器:
vm.getProduct = function ($viewValue) {
return DataService.getProductTypeAhead($viewValue, function (response) {
console.log(response);
return cleanResponse(response);
});
};
function cleanResponse(response) {
return JSON.parse(global.angular.toJson(response));
}
HTML:
<input type="text" class="form-control" id="product"
typeahead-min-length="3"
typeahead="product as product.legalName for product in vm.getProduct($viewValue)"
typeahead-template-url="app/main/templates/typeahead-ProductInterface-Template.html"
ng-model="vm.trade.PRODUCT_NAME">
然而,如果我采用$http.get()方法,我可以在HTML中看到数组。
vm.getProduct1 = function ($viewValue) {
return $http.get('/productinterface?typeahead=' + $viewValue).then(function (response) {
console.log(response.data);
return response.data;
});
};
我检查了同步$resource调用的帖子。在这方面运气不佳。任何关于这个问题的建议都将不胜感激。
TIA,Bhushan
如果您想将$resource
与typeahead
一起使用,则不能使用普通回调。你需要回报一个承诺。
返回$resource
请求的承诺:
vm.getProduct = function ($viewValue) {
return DataService.getProductTypeAhead($viewValue, function (response) {
return response;
}).$promise;
};
返回一个带有回调的$resource
请求的promise;
vm.getProduct = function ($viewValue) {
return DataService.getProductTypeAhead($viewValue)
.$promise.then(function(response) {
return doSomethingWith(response);
});
});
};
参见示例-异步结果
您返回的是承诺而不是值。这就是vm.getProduct是一个返回promise而非数据的函数。
vm.getProduct = function ($viewValue) {
return DataService.getProductTypeAhead($viewValue, function (response) {
console.log(response);
return cleanResponse(response);
});
};
function cleanResponse(response) {
return JSON.parse(global.angular.toJson(response));
}
您应该将其设置为任何vm属性,如
vm.getProduct = function ($viewValue) {
return DataService.getProductTypeAhead($viewValue, function (response) {
console.log(response);
vm.myProperty = cleanResponse(response);
return cleanResponse(response);
});
};
相关文章:
- 从HTML调用typescript文件中的函数
- 从index.html调用函数,该函数无限循环
- 为什么可以't在对指令的html调用中访问$rootScope
- 从周围的 HTML 调用 SVG 中的 JavaScript 函数
- 无法从index.html调用javascript函数
- 从html调用angularjs控制器中的一个函数,但未定义范围变量
- AJAX使用从PHP生成的HTML调用的函数更新MYSQL数据库
- angularjs从html调用函数返回值
- 通过JS和html调用REST API
- 从HTML调用babel脚本中定义的js函数
- 我应该如何从 html 调用 javascript 类方法
- 使用 quick.js 从 HTML 调用 JavaScript 函数
- 无法从 HTML 调用 Javascript 类
- 如何从.js文件中的.html调用 JavaScript 函数
- 从 html 调用 jquery 函数进行解析
- 从 HTML 调用节点请求
- 从动态 HTML 调用本地 JavaScript 函数
- 从 HTML 调用 JavaScript 函数
- 从 html 调用 javascript 脚本
- 从 HTML 调用 WCF 服务总是转到错误函数.适用于 aspx 页面