當您的角度應用準備好DOM準備就緒時,角度場景運行良好。使用requirejs或其他AMD lib時情況並非如此。如何在角度場景中添加對AMD的支持?如何在requirejs中使用角度場景
回答
你必須做的是覆蓋默認的幀加載行爲,並在你的應用程序準備就緒時發出新的事件。
第一件事是在角度應用程序中添加以下幾行以及angular.bootstrap調用。角度場景需要此數據。
angular.bootstrap(document, ['app']);
var html = document.getElementsByTagName('html')[0];
html.setAttribute('ng-app', 'app');
html.dataset.ngApp = 'app';
if (top !== window) {
top.postMessage({
type: 'loadamd'
}, '*');
}
接下來,在您的e2e跑步者中,您必須包含這些線。如果它是一個外部腳本,它必須角方案之後被加載,它必須被解析之前DOM已準備就緒:
/**
* Hack to support amd with angular-scenario
*/
(function() {
var setUpAndRun = angular.scenario.setUpAndRun;
angular.scenario.setUpAndRun = function(config) {
if (config.useamd) {
amdSupport();
}
return setUpAndRun.apply(this, arguments);
};
function amdSupport() {
var getFrame_ = angular.scenario.Application.prototype.getFrame_;
/**
* This function should be added to angular-scenario to support amd. It overrides the load behavior to wait from
* the inner amd frame to be ready.
*/
angular.scenario.Application.prototype.getFrame_ = function() {
var frame = getFrame_.apply(this, arguments);
var load = frame.load;
frame.load = function(fn) {
if (typeof fn === 'function') {
angular.element(window).bind('message', function(e) {
if (e.data && e.source === frame.prop('contentWindow') && e.data.type === 'loadamd') {
fn.call(frame, e);
}
});
return this;
}
return load.apply(this, arguments);
}
return frame;
};
}
})();
最後,爲了使AMD配置,您必須將屬性NG-useamd添加到角場景的腳本標記。
<script type="text/javascript" src="lib/angular-1.0.3/angular-scenario.js" ng-autotest ng-useamd></script>
現在你可以去
與角度的場景V1.0.3
以上回答部分是我的方案失敗(角1.1.4,新鮮噶)進行測試。 在調試視圖中,它運行正常,在正常概覽頁面失敗。我注意到一個額外的嵌套。
我更改了代碼以將消息發送到測試應用程序的父級iframe。
if (top !== window) {
window.parent.postMessage({
type: 'loadamd'
}, '*');
}
是的,這是一個很好的觀點。噶創建另一個框架,其中包括角度的情節框架,其中包括你的應用程序的框架。所以在這種情況下,它確實是父框架而不是頂框架。 – Francis 2013-05-14 19:55:21
接受的答案是絕對正確的,但遺憾的是您必須將該代碼放入您的頁面。
所以,如果有幫助的話,我創建了一個業力預處理程序來注入'修復'作爲測試運行的一部分。
代碼:https://github.com/tapmantwo/karma-ng-bootstrap-fix-preprocessor NPM:https://www.npmjs.org/package/karma-ng-bootstrap-fix-preprocessor
注意,如果你是通過因果報應服務中的文件這僅適用。如果你是代理他們,他們不通過預處理器。因此,作爲替代方案,我有一個ng-scenario的分支,它可以執行類似的操作,以允許手動引導的網站運行;
https://github.com/tapmantwo/karma-ng-scenario
這不是一個節點模塊,所以你必須手動設置它,但它的更好(恕我直言)有到位這樣的事情,而不是傳染的生產代碼的東西只是爲了讓測試通過。
- 1. 如何在場景中使用INNER JOIN?
- 2. 有角度的畫布(Three.js)場景
- 3. 角度2+「組件加載...」場景
- 4. 如何在角度應用程序中使用requirejs來要求dojox?
- 5. 如何使用前一場景中的數據執行場景?
- 6. 如何在JavaFX中將場景添加到場景中?
- 7. A-Frame:如何使用場景動畫場景上的物體
- 8. 如何在此場景中應用MVVM
- 9. 如何在角度cli中使用PostCSS
- 10. 如何在角度服務中使用$?
- 11. 如何在場景中放置場景 - Sprite Kit
- 12. 如何使用角度Angucomplete整合FourSquare場地?
- 13. 如何以正確的標準處理`角度路線「中的不同場景?
- 14. 角度令牌和用戶登錄場景
- 15. 在irrlicht場景中設置透明度
- 16. 如何使用RequireJS
- 17. JavaFX在場景中嵌入場景
- 18. 如何加載collada場景(使用three.js)?
- 19. 如何在我的場景中獲取父元素高度?
- 20. 如何在多個文件上運行一個Cucumber(使用量角器)場景?
- 21. 如何在角度2中進行多場驗證?
- 22. 角度應用程序可以部分使用requirejs嗎?
- 23. 如何使角度控制器在整個場地都可重複使用?
- 24. 如何在RequireJS中使用NodeJS包?
- 25. 如何在RequireJS中使用Magento2?
- 26. 如何在requirejs中使用jquery.validate?
- 27. 如何在RequireJS中使用VueJS?
- 28. 如何在RequireJS中使用Bing Maps API?
- 29. 如何使用角度
- 30. 如何使用角度
任何機會,你可以提供一個github回購或小提琴? – 2013-08-16 15:34:49
非常感謝弗朗西斯。需要注意的是,如果出於任何原因在runner頁面中包含JQuery,則需要在到達時解開消息事件,如:if(e.originalEvent)e = e.originalEvent; – 2013-09-06 06:21:37