AngularJS控制器之间的通信

时间:2018-03-22 14:09:19

标签: angularjs coffeescript angularjs-controlleras

我试图从第二个控制器触发一个控制器中定义的操作。我已经通过了this question,但我无法看清我做错了什么。这是代码:

window.cartApp = angular.module 'cartApp', ['pascalprecht.translate', 'directives', 'cartServices',
    'angular-click-outside']

class CartCtrl
    constructor: (@$scope, @$http, @$timeout, @$filter, PriceOption, EnableMealsService, CookiesService) ->
        @$scope.cartItems = {'meals': [], 'submeals': []}
        @$scope.Math = window.Math;

        _scope = @$scope

        _http = @$http
        window.cartApp.config(window.translate)
        @$scope.$on('triggerGetQuote', () -> console.log('getting quote'))


class IndexCtrl
    constructor: (@$scope, @$rootScope, @$http, CookiesService) ->
        _scope = @$scope
        _rootScope = @$rootScope
        _http = @$http
        @$scope.getLocation = () ->
            console.log('Before broadcast of triggerGetQuote')
            _scope.$broadcast('triggerGetQuote')


CartCtrl.$inject = ['$scope', '$http', '$timeout', '$filter', 'PriceOption', 'EnableMealsService', 'CookiesService']
IndexCtrl.$inject = ['$scope', '$rootScope', '$http', 'CookiesService']
window.cartApp.controller 'CartCtrl', CartCtrl
window.cartApp.controller 'IndexCtrl', IndexCtrl

所以我希望在我的控制台中记录这两个事件,但只触发第一个:

  

在广播triggerGetQuote之前

0 个答案:

没有答案