热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何使用带有requirejs的角度场景-Howtouseangular-scenariowithrequirejs

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 个解决方案

#1


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进行测试

#2


4  

The above answer failed partly in my scenario (Angular 1.1.4, fresh Karma). In the debug view it ran fine, in the normal overview page it failed. I noticed an extra nested .

上面的答案在我的场景中部分失败了(Angular 1.1.4,新鲜的Karma)。在调试视图中它运行正常,在正常的概述页面中失败了。我注意到了一个额外的嵌套。

I changed the code to message to the parent iframe of the tested application.

我将代码更改为消息到测试应用程序的父iframe。

if (top !== window) {
    window.parent.postMessage({
        type: 'loadamd'
    }, '*');
}

#3


1  

The accepted answer is absolutely correct, but it's a shame that you have to put that code into your page.

接受的答案是绝对正确的,但是你必须将这些代码放入你的页面是一种耻辱。

So, if it helps, I created a preprocessor for karma to inject the 'fix' in as part of the test run.

因此,如果它有所帮助,我为业力创建了一个预处理器,以便在测试运行中注入“修复”。

Code: https://github.com/tapmantwo/karma-ng-bootstrap-fix-preprocessor npm: https://www.npmjs.org/package/karma-ng-bootstrap-fix-preprocessor

代码:https://github.com/tapmantwo/karma-ng-bootstrap-fix-preprocessor NPM:https://www.npmjs.org/package/karma-ng-bootstrap-fix-preprocessor

Note, this only works if you are serving the files through karma. If you are proxying them, they dont go through the pre-processor. So, as an alternative I have a fork of ng-scenario which does something similar to allow manually bootstrapped sites to run;

请注意,这仅适用于通过业力提供文件的情况。如果你代理他们,他们不会通过预处理器。所以,作为一种替代方案,我有一个ng-scenario的分支,它做了类似的事情,允许手动引导的站点运行;

https://github.com/tapmantwo/karma-ng-scenario

https://github.com/tapmantwo/karma-ng-scenario

This isn't a node module, so you'll have to set it up manually, but its better (IMHO) to have something like this in place instead of infecting production code with something just to get tests to pass.

这不是一个节点模块,所以你必须手动设置它,但它更好(恕我直言)有这样的东西,而不是感染生产代码只是为了让测试通过。


推荐阅读
  • <head><scriptlanguagejavascriptsrcrequire.jsdata-mainscriptsmain> ... [详细]
  • 本文介绍了2015年九月八日的js学习总结及相关知识点,包括参考书《javaScript Dom编程的艺术》、js简史、Dom、DHTML、解释型程序设计和编译型程序设计等内容。同时还提到了最佳实践是将标签放到HTML文档的最后,并且对语句和注释的使用进行了说明。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 本文总结了在编写JS代码时,不同浏览器间的兼容性差异,并提供了相应的解决方法。其中包括阻止默认事件的代码示例和猎取兄弟节点的函数。这些方法可以帮助开发者在不同浏览器上实现一致的功能。 ... [详细]
  • 如何解决《为什么chrome.tabs.query()在Chrome扩展程序中使用RequireJS调用时会返回标签的URL?》经验,为你挑选了1个好方法。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了django中视图函数的使用方法,包括如何接收Web请求并返回Web响应,以及如何处理GET请求和POST请求。同时还介绍了urls.py和views.py文件的配置方式。 ... [详细]
  • 本文介绍了绕过WAF的XSS检测机制的方法,包括确定payload结构、测试和混淆。同时提出了一种构建XSS payload的方法,该payload与安全机制使用的正则表达式不匹配。通过清理用户输入、转义输出、使用文档对象模型(DOM)接收器和源、实施适当的跨域资源共享(CORS)策略和其他安全策略,可以有效阻止XSS漏洞。但是,WAF或自定义过滤器仍然被广泛使用来增加安全性。本文的方法可以绕过这种安全机制,构建与正则表达式不匹配的XSS payload。 ... [详细]
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • 第8章 使用外部和内部链接
    8.1使用web地址LearnAboutafricanelephants. ... [详细]
  • Vue基础一、什么是Vue1.1概念Vue(读音vjuː,类似于view)是一套用于构建用户界面的渐进式JavaScript框架,与其它大型框架不 ... [详细]
  • python+selenium十:基于原生selenium的二次封装fromseleniumimportwebdriverfromselenium.webdriv ... [详细]
  • 如何解决《Angularjs:ocLazyLoadvsRequirejs》经验,为你挑选了2个好方法。 ... [详细]
  • 在angular与requirejs的结合中,提示“Modulenamehasnotbeenloadedyetforcontext:_”,如下:require-lib ... [详细]
author-avatar
龙叔君_541
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有