[英]vaadin: integrate angular js using custom layout
我試圖通過使用這樣的自定義布局將角度js應用程序集成到vaadin中:
VerticalLayout mainLayout = new VerticalLayout();
mainLayout.setMargin(true);
mainLayout.setWidth("1380px");
setCompositionRoot(mainLayout);
Panel panel = new Panel();
CustomLayout layout = null;
try {
String dynamicHtml = "<div ng-app=\"app\">..........</div>";
layout =
new CustomLayout(new ByteArrayInputStream(dynamicHtml.getBytes()));
} catch (IOException e) {
logger.error("could not create custom layaout", e);
}
panel.setContent(layout);
mainLayout.addComponent(panel);
importJs();
public void importJs() {
String PATH_TO_JS_SCRIPT = jsURL+"?tata=" + new Date();
String script = "try{var fileref=document.createElement('script');";
script += "fileref.setAttribute(\"type\",\"text/javascript\");";
script += "fileref.setAttribute(\"src\", \"" + PATH_TO_JS_SCRIPT +
"\");";
script += "document.getElementsByTagName(\"head
\")[0].appendChild(fileref);}catch(e){alert(e);}";
Page.getCurrent().getJavaScript().execute(script);
}
我使用importJs方法導入腳本js。 第一次訪問該頁面時,js文件已加載並且可以工作。 第二次,它不起作用。 好像沒有導入腳本。 我看不到代碼有什么問題嗎? 使用Javascript注釋我也有同樣的問題。 我使用vaadin 7.6.5 Java 7
在CustomLayout中插入角度代碼不是一個好主意。
我建議閱讀文本: Vaadin和AngularJS-在一起開心 。
Js文件僅包含一次,並且在頁面生命周期中僅包含一次。 要在每次顯示我使用的頁面時執行此腳本:
if(typeof angular != 'undefined'){
angular.bootstrap(document.getElementById('swdId'), ['app']);"
}
問題已解決
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.