[英]Convert jQuery post with callback functions to angular $http
I have a angular controller that has a function which adds the html retrieved in post to a container. 我有一个角度控制器,该控制器具有一个函数,该函数会将在帖子中检索到的html添加到容器中。 I can then click on a div within that sets a var result to some data onclick.
然后,我可以在其中将var结果设置为onclick某些数据的div上单击。
<div onclick="var result = {"some":"data", "right":"here"}...
The issue Im having is retaining the callback functions functionality. 我有的问题是保留回调函数功能。 This is what I am trying to convert.
这就是我要转换的内容。
jQuery.post(url, {
'callback': "callbackFunctions.setData(result);callbackFunctions.closeDialog();",
'return_type' : 'json'
},
function (json) {
if (json.success) {
jQuery('#template').html(json.message);
}
}, 'json');
That snippet is currently using a external set of functions from the angular controller. 该代码段当前正在使用角度控制器的一组外部功能。
var callbackFunctions = {
closeDialog: function () {
angular.element('#template').dialog('close');
},
SetData: function (result) {
var scope = angular.element('.variables img:visible').scope().$parent;
scope.$apply(function(){
jQuery.extend(true, scope.variables, {
url: result.url,
name: result.name,
thumbnail: result.thumbnail,
value: result.url
});
});
}
};
I just don't think this is the cleanest solution. 我只是认为这不是最干净的解决方案。 It currently works I just cant seem to convert this to angular's $http request and get the callback functions to work.
它目前有效,我似乎无法将其转换为angular的$ http请求,并使回调函数正常工作。 Ideally this would be $http.post and when calling the functions they would be within the controller so I already have access to the scope.
理想情况下,这将是$ http.post,并且在调用函数时它们将位于控制器内,因此我已经可以访问作用域。
One gotcha I can not modify the http I am requesting its used in a ton of other areas of the site not yet updated. 我无法修改所请求的http,这是我在许多尚未更新的网站其他区域中使用的http的要求。
Controller 控制者
$http.post(url, data).then(function(response){
$scope.message = response.message;
}, function(error){
console.log("Error detected : " + error);
})
HTML 的HTML
<div id="template">
{{message}}
</div>
I don't really know what you want to do with the API's data but you can use ng-bind
or {{variable}}
inside your html to display them 我真的不知道您想对API的数据做什么,但是您可以在html中使用
ng-bind
或{{variable}}
来显示它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.