Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why $routeChangeSuccess never gets called?

I am doing something similar to below on my app but I am just not able to get the routeChangeSuccess event.

var myapp = angular.module('myapp', ["ui.router", "ngRoute"]);

myapp.controller("home.RootController",
  function($rootScope, $scope, $location, $route) {
    $scope.menus = [{ 'name': 'Home ', 'link': '#/home'}, {'name': 'services', 'link': '#/services'} ]

    $scope.$on('$routeChangeSuccess', function(event, current) {

      alert('route changed');
    });

  }
);

myapp.config(
  function($stateProvider, $urlRouterProvider, $routeProvider) {
    $urlRouterProvider.otherwise("/home");
    $stateProvider
      .state('home', {
        url: "/home",
        //template: '<h1>Home Screen</h1>'
        templateUrl: "/Client/Views/Home/Home.htm"
      })
      .state('services', {
        url: "/services",
        //template: '<h1>Service screen</h1>'
        templateUrl: "/Client/Views/Home/service.htm"

      });
  });

a very simple html as below also fails

  <body ng-controller="home.RootController">

    <ul class="nav">
      <li ng-repeat="menu in menus" "="">
        <a href="{{menu.link}}">{{menu.name}}</a>
      </li>
    </ul>
    <div ui-view> No data yet!</div>
  </body>

but when i click on the link i see that the views are getting updated but the $routeChangeSucces event is never triggered.

is there something i am missing?

Another question I had was is there an event that I can hook on to to know the view is ready so I can start some additional processing, like document.ready().

plnlr but not fully working...

Regards Kiran

like image 840
Kiran Avatar asked Dec 08 '13 14:12

Kiran


2 Answers

Please, check this wiki: State Change Events. An extract:

  • $stateChangeSuccess - fired once the state transition is complete.

    $scope.$on('$stateChangeSuccess',
    function(event, toState, toParams, fromState, fromParams){ ... })

So instead of the $routeChangeSuccess use the $stateChangeSuccess.

To get more detailed information about all available events, check the wiki Events. Here you can find that the suitable for you, could be event $viewContentLoaded...

like image 92
Radim Köhler Avatar answered Oct 21 '22 07:10

Radim Köhler


stateChange events are now deprecated and removed, use transitions instead.

$transitions.onSuccess({}, function () {
    console.log("state changed");
});
like image 38
Ohad Cohen Avatar answered Oct 21 '22 06:10

Ohad Cohen