我真的是新的角度。 即时通讯使用角1.6,我需要做一些现有的应用程序的变化。指令角
正如我可以搜索,以前的开发人员使用http://www.codingdrama.com/bootstrap-markdown/有一个textarea与一些文本选项和预览。我现在的任务是覆盖预览按钮,以插入文本和API返回一些结果来调用我们的API。关于那个降价的文档,我加了起来,我发现了这个。
onPreview:功能(E){VAR previewContent
if (e.isDirty()) {
var originalContent = e.getContent()
previewContent = "Prepended text here..."
+ "\n"
+ originalContent
+ "\n"
+"Apended text here..."
} else {
previewContent = "Default content"
}
return previewContent
},
所以我开始重写:
app.directive("markdowntextarea", ['$http', function ($http) {
return {
link: function (el_scope, element, attr) {
var previewContent = "preview";
element.markdown(
{
autofocus: false,
savable: false,
onPreview: function (e) {
console.log('1');
if (e.isDirty()) {
console.log('2!!')
var originalContent = e.getContent();
$http({
url: '/api/markdown/',
data: {"body": originalContent, "actual_format": "md", "desire_format": "html"},
method: 'POST'
}).then(function successCallback(response) {
console.log(response.data.content);
previewContent = response.data.content;
});
}else{
console.log('3')
previewContent = "";
}
previewContent = 'test';
return previewContent;
},
});
}
}
}]);
我无法找到我的错误,但previewContent总是返回“预览”。从API方面来说还行,response.data.content
也是正确的。
不知道下一步该怎么做
@ gus27分号在javascript中是可选的。 – Lex
阅读关于诺言和异步调用... –
好吧,我将预览内容移到IF之外 – 62009030