- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 abc
Controller 中有以下代码:
$rootScope.$on('selectedItem', function (event, data) {
vm.selectedItem = data;
});
调用函数在 xyz
Controller 中:
function doThis(){
$rootScope.$emit('selectedItem', 'somedata');
}
如何在 karma 测试中重现或模拟这种情况?
最佳答案
对于第一个 Controller (abc
),您可以使用$rootScope.$on
收听它,您可以先$rootScope.$emit
它和 $scope.$digest()
它。这样您就可以在 $on
中收到它。
var rootScope;
beforeEach(inject(function(_$rootScope_) {
rootScope = _$rootScope_;
}));
describe("some function", function() {
it("should receive selectedItem with $on", function() {
rootScope.$emit('selectedItem', 'somedata');
$scope.$digest();
expect(vm.selectedItem).toEqual('somedata');
});
});
对于第二个 Controller (xyz
),您可以在 $rootScope.$emit
上监视
。并且 expect
它在 xyz
Controller 中被调用。像这样:
var rootScope;
beforeEach(inject(function(_$rootScope_) {
rootScope = _$rootScope_;
spyOn(rootScope, '$emit');
}));
describe("doThis function", function() {
it("should $emit selectedItem", function() {
vm.doThis(); // or if you use $scope, call it that way
expect(rootScope.$emit).toHaveBeenCalledWith('selectedItem');
});
});
关于javascript - 如何测试 $rootScope.$emit 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43135747/
为什么在此示例中需要 $rootScope.$apply() 来使用页面上的 ng-hide 更新元素? 根据我的经验,每当我将 $scope.$apply() 放入 $scope.$watch 中时
$rootScope 和$rootScope.$root 有区别吗? 有什么区别 $rootScope.global.flag = true 和 $rootScope.$root.global.fla
我想在我的应用程序中共享这些功能,而我的工厂很少。问题是我必须在需要时在每个 Controller 中添加所有这些工厂。随着工厂和 Controller 数量的增加,这是一项乏味的任务! 因此我试图找
我在 $rootScope 上定义了一个方法 $rootScope.method = function(value){ // wanna give this value to `$scope.
我想知道我是否可以在 $rootScope.$on 函数中调用 $rootScope.$emit,原因是我有两个 Controller ctrl1.js 和`ctrl2.js,我想在我的 ctrl1.
谁能帮助理解 $rootScope.$new() 和 $rootScope.$new(true) 之间的区别?根据我的理解,它们应该是相同的,因为 $rootScope 没有父作用域。 最佳答案 $n
在广播和接收广播方面, Angular 1.2 rc2 和 Angular 1.2 之间有什么变化吗? 我有我认为是 $watch Controller 中的标准广播,即 $scope.$wa
我在我的应用程序中将 UI-router 用于嵌套 View ,但同时我想在路由更改时监听事件。在使用 UI-router routeChangeSuccess 之前,它触发得很好,但在 ui-rou
嗨,我想知道在性能方面什么更好。 假设我有一个广播东西的工厂: angular.module('core.foo') .factory('Foo', ['$rootScope',
我在 App.run 中定义了一些模型下面我在 Controller 中覆盖 someCtrl : App.run(['$rootScope', function($rootScope) { $r
我的应用出现以下错误 Error: [$rootScope:infdig] http://errors.angularjs.org/1.4.1/$rootScope/infdig?p0=10&p1=%
在一个 Controller 中,我这样做: $rootScope.$emit("newAction", {}); 在另一个 Controller 我做: $rootScope.$on('newAct
我想将数据从一个 Controller 发送到另一个 Controller 。其实我已经关注了这个link 我的第一个 Controller 是这样的: class ProductCtrl const
用户注销后,我需要清理我的 $rootScope。我试过 $rootScope.$destroy()但这并没有奏效。有没有办法遍历 $rootScope 中的所有值并删除它们或简单地重置它的方法? 最
抱歉,我的英语不好。我有 angularjs 应用程序,有很多页面和表单。对于任何页面,使用该页面的模型创建文件。示例: file userDict.js: $rootScope.dicts.user
当互联网不可用时,我想在每个页面上显示互联网不可用栏。 我正在使用 Ionic 框架,因此决定将代码保留在 menu.html 上 这是我的菜单 html 代码 Internet not availa
我在我的 Controller $rootScope.$on 函数中监听用户登录 $rootScope.$on('$firebaseSimpleLogin:login',functio
我在 Controller 中使用 $rootScope.$on 来监听我正在使用的模块(Angular-packery)发出的事件。它正在工作,它获取事件并且我可以在“args”上执行操作。但我想做
以下文件“有效”(即它不会引发任何错误): angular.module("modx", [], function($routeProvider) {
我有一个更新 $rootScope 的 Angular 服务。更新实际上工作正常,但它在控制台中抛出一个错误,这让我很担心。 app.service("scroll", function($rootS
我是一名优秀的程序员,十分优秀!