0

I am using $scope.$emit in my ListController to trigger a $rootScope.$on event in the MainController(angular 1.3):

(function () {
    'use strict';

    angular
        .module('mymodule.faultlist')
        .controller('MainController', ['$rootScope', function ($rootScope) {
            $rootScope.$on('newViewLoaded', function (event, metadata) {
                $rootScope.metadata = metadata;
                console.log('hello metacontroller metadata', metadata);
            });

        }])
        .controller('ListController', ['faultlistservice', 'faultListConstants', '$scope', '$timeout', '$rootScope',
            function (faultlistservice, faultListConstants, $scope, $timeout, $rootScope) {

                $timeout(function () {
                    $scope.$emit('newViewLoaded', {
                        'title': faultListConstants.meta.title,
                        'description': faultListConstants.meta.description
                    });
                }, 100);

            }]);
})();

This is what the index.html roughly looks like:

.
.
<head ng-controller="MainController">
<div class="container main-content" ui-view >
</div>
..

This does not get triggered. I tried moving the $rootScope.$on block to the ListController and then the $rootScope.$on gets triggered. Why is this not working for the MainController or is there a better way of implementing this?

bier hier
  • 20,970
  • 42
  • 97
  • 166

2 Answers2

0

your code works http://plnkr.co/edit/hsl3l6rTJd1SUDgaijCy?p=preview

I think your problem might be in that you have ng-app not on html tag, so <head ng-controller="MainController"> this line is outside ngApp.

Petr Averyanov
  • 9,327
  • 3
  • 20
  • 38
0

your code works properly http://plnkr.co/edit/nX9KVqY0SZ46Yvf92VAk?p=preview

The plunkr shows how you can you use this example to work with your original code. The title and description over here are arguments to be passed inside the emit function and cannot be string literals

 $scope.$emit('newViewLoaded', {
                        'title': faultListConstants.meta.title,
                        'description': faultListConstants.meta.description
                    })
Deepanjan
  • 649
  • 2
  • 8
  • 15