sulu666 sulu666 - 1 year ago 154
AngularJS Question

Reload one controller from another controller Anjular js

I am new to Angular js.I have seen the similar question, but I dont understand that.
I have 2 controllers

userControllers .controller('RatingCtrl', function($scope,$http,$rootScope,$route)
userControllers.controller('otherProfileCtrl', function ($scope, $routeParams, $rootScope, $http, $location, $window, $timeout,$uibModal, $compile)

RatingCtrl and otherProfileCtrl, this two modules are inter-related. My need is that, I have reload RatingCtrl from otherProfileCtrl using
.Is there is any way to do this without uisng service?plz help

Answer Source

You could pass events from one controller to another in order to achieve this. You would then do something like:

var app = angular.module('myApp', []);

app.controller('firstController', ['$scope', '$rootScope',
  function($scope, $rootScope) {

    $scope.text = 'Initial text';
    $scope.changeText = function(message) {
      $scope.text = message;

    $rootScope.$on('customEvent', function(event, message) {


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

    $scope.message = 'Message from second controller';

    $scope.sendEvent = function() {
      $scope.$emit('customEvent', $scope.message)

<script src=""></script>
<div ng-app="myApp">
  <div ng-controller="firstController">
    <h2>This is the fist controller:</h2>
  <div ng-controller="secondController">
    <h2>This is the second controller:</h2>
    <input type="text" ng-model="message" />
    <button ng-click="sendEvent()">Send message</button>


Here, the firstController listens to events propagated to the $rootScope, and the secondController sends the message. That is the functionality that you are looking for.

That being said, you would be much better off implementing shared behaviour in a service, since keeping track of all your custom events can be particularly tough.

Hope this helps.