作者:龙叔君_541 | 来源:互联网 | 2023-05-19 05:26
Angular-scenarioworkswellwhenyourangularappisreadyonDOMready.Isnotthecasewhenusing
Angular-scenario works well when your angular app is ready on DOM ready. Is not the case when using requirejs or an other AMD lib. How to add support for AMD in angular-scenario?
当角度应用程序准备就绪时,Angular-scenario可以很好地工作。使用requirejs或其他AMD lib时不是这种情况。如何在角度场景中添加对AMD的支持?
3 个解决方案
9
What you have to do is to override the default frame load behaviour and to emit a new event when you amd app is ready.
您需要做的是覆盖默认的帧加载行为,并在应用程序准备就绪时发出新事件。
The first thing is to add the following lines in your angular application along with the angular.bootstrap call. This data is required by angular-scenario.
第一件事是在angular应用程序中添加以下行以及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'
}, '*');
}
Next, in your e2e runner, you have to include those lines. If it's an external script, it must be loaded after angular-scenario and it must be parsed before the DOM is ready :
接下来,在您的e2e跑步者中,您必须包含这些线。如果它是外部脚本,则必须在angular-scenario之后加载它,并且必须在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;
};
}
})();
Finally, to enable the amd configuration, you must add the attribute ng-useamd to angular-scenario's script tag.
最后,要启用amd配置,必须将属性ng-useamd添加到angular-scenario的脚本标记中。
You're now ready to go
你现在准备好了
tested with angular-scenario v1.0.3
使用角度场景v1.0.3进行测试