$并在$播出角 [英] $on and $broadcast in angular

查看:115
本文介绍了$并在$播出角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个footerController和codeScannerController有不同的看法。

I have a footerController and codeScannerController with different views.

angular.module('myApp').controller('footerController', ["$scope", function($scope) {}]);

angular.module('myApp').controller('codeScannerController', ["$scope", function($scope) {
console.log("start");
$scope.startScanner = function(){...

当我点击一个<李方式> 在footer.html我应该在codeScannerController此事件

When I click on a <li> in footer.html I should get this event in codeScannerController.

<li class="button" ng-click="startScanner()">3</li>

我觉得可以用 $关于 $广播,但我不知道该怎么和实现找不到任何地方的例子。

I think it can be realised with $on and $broadcast, but I don't know how and can't find examples anywhere.

推荐答案

如果你想 $广播使用 $ rootScope

$scope.startScanner = function() {

    $rootScope.$broadcast('scanner-started');
}

然后接收,使用 $范围控制器的:

$scope.$on('scanner-started', function(event, args) {

    // do what you want to do
});

如果你愿意,你可以传递参数,当你 $广播

If you want you can pass arguments when you $broadcast:

$rootScope.$broadcast('scanner-started', { any: {} });

然后接受他们:

$scope.$on('scanner-started', function(event, args) {

    var anyThing = args.any;
    // do what you want to do
});

文档内部文档这一点。

这篇关于$并在$播出角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆