即时通讯使用这个插件:http://www.codingdrama.com/bootstrap-markdown/无极Angularjs 1.6如何强制函数等待API响应
我想钩onPreview
于是就onPreview
我尽量让我的API调用:
app.directive("markdowntextarea",function ($http, $q) { // inject $q
return {
link: function (el_scope, element, attr) {
element.markdown(
{
autofocus: false,
savable: false,
onPreview: function (e) {
var deferred = $q.defer();
if (e.isDirty()) {
var originalContent = e.getContent();
$http({
url: '/api/markdown/',
data: {"body": originalContent, "actual_format": "md"},
method: 'POST'
}).then(function successCallback(response) {
console.log("successCallback", response.data.content);
deferred.resolve(response.data.content);
}, function errorCallback(response) {
console.log("errorCallback");
deferred.reject("error");
});
} else {
deferred.resolve("");
}
return deferred.promise;
}
}
);
}
}
});
控制台:
successCallback from api!!!
我从api获得了成功响应,response.data.content就是我想要使用的。这里的问题是return deferred.promise;
始终返回原始值。我能在这里做什么?我真的是新角色
的文档您的questrion是什么?你想要什么? – DAG
我想将我的呼叫回复给api。目前我无法返回,始终返回原始值。谢谢 – 62009030
您只需将它称为onPreview()。然后(()=> {})。你是这个意思吗?否则你的问题是不完整的,我猜。发布完整的代码! – DAG