My Blog List

Monday, March 24, 2014

Communication Between Controllers~!

This is the way that
how Controllers are communicating with each other

summary
call $rootScope.$broadcast of Service and
   other controller will listen $scope.$on





1. Service Registation 

var app= angular.module('myApp', ['onsen.directives', 'ngTouch']);
app.factory('messageService', function ($rootScope) {
    var messenger = {
        messages: [],
        identity: 0,

        addMessage: function (tab) {
            $rootScope.$broadcast('messageAdded', tab.url);
        }
    };
    return messenger;
});

2. Controller Sending

app.controller('titleCtrl', ['$scope','messageService',  function ($scope, messageService) {

 $scope.onClickTab = function (tab) {
            $scope.currentTab = tab.url;
            console.log('dd');
            messageService.addMessage(tab);

            

        }
}

2.  Controller Receiving

app.controller('tabsCtrl', ['$scope', function ($scope, myService) {
$scope.$on("messageAdded", function (event, args) {
            console.log('myEvent');
            console.log('args : ' + args);
            $scope.currentTab =args;

        });
}

No comments:

Post a Comment