Prathamesh Rasam
Prathamesh Rasam

Reputation: 436

How i will get to know in routeChangeStart event event triggered by JS or click

Main.js

$routeProvider
    .when('/home', {
        templateUrl: 'home.html',
        controller: 'StudentController'
    })
    .when('/viewStudents', {
        templateUrl: 'viewStudents.html',
        controller: 'StudentController'
    })
    .when('/viewTeacher', {
        templateUrl: 'viewTeacher.html',
        controller: 'StudentController'
    })
    .otherwise({
        redirectTo: '/home'
    });

Code in another jS

   $rootScope.$on("$routeChangeStart", function(event, next, current){
                console.log(event);
                //here i want to detect         
            });

when user visit index.html home route fired from JS & home.html added in view

.otherwise({
            redirectTo: '/home'
        });

there is button which call viewStudents.html then route will change and viewStudents.html will rendered

how i will get in routeChangeStart function that route is changing due to jS or click by user

Upvotes: 0

Views: 370

Answers (1)

Raj Rusia
Raj Rusia

Reputation: 736

routeChangeStart event triggered by either '$rootScope.$emit' or $rootScope.$broadcast

use of this event system is to pass the data child controller to parent.

when you call the broadcast event then $on event will call.

it's call once when your controller load. and you can call it externally using funtion.

    app.controller('ChildCtrl',
      function ChildCtrl ($rootScope) {

      $rootScope.$emit('rootScope:emit', 'Emit!'); // $rootScope.$on
      $rootScope.$broadcast('rootScope:broadcast', 'Broadcast'); // $rootScope.$on && $scope.$on

    });

app.controller('ParentCtrl',
  function SiblingOneCtrl ($rootScope) {
    var myListener = $scope.$on('child', function (event, data) {
        // do something
      });
    });

app.controller('ParentCtrl',
  function ParentCtrl ($scope) {

    var myListener = $rootScope.$on('child', function (event, data) {
        // do something
      });
});

Upvotes: 2

Related Questions