Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a footerController and codeScannerController with different views.

(我有一个footerController和codeScannerController具有不同的视图。)

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

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

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

(当我点击footer.html中的<li> ,我应该在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.

(我认为它可以通过$on$broadcast来实现,但我不知道怎样也无法在任何地方找到示例。)

  ask by Alice Polansky translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you want to $broadcast use the $rootScope :

(如果你想要$broadcast使用$rootScope :)

$scope.startScanner = function() {

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

And then to receive, use the $scope of your controller:

(然后接收,使用控制器的$scope :)

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

    // do what you want to do
});

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

(如果你想要,你可以在$broadcast时传递参数:)

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

And then receive them:

(接收它们:)

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

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

Documentation for this inside the Scope docs .

(Scope文档中的文档 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...