1

I have a problem with app.run(). In the main page app.run() is working. When I click to the menu button Events and in the page events.html app.run() doesn't work. But when I reload this page app.run() is working. I don't know what to do. Pages main.html and events.html are in the ngView, menu also is in the ngView.
controller.js

afroEarthApp.config(['$routeProvider', function($routeProvider){
    $routeProvider
      .when('/',{
        templateUrl:'template/home.html',
        controller:'afroEarthMainCtrl'
      })
      .when('/events',{
        templateUrl:'template/events.html',
        controller:'eventsCtrl'
      })
      .when('/sites/:niche', {
        templateUrl:'template/single.html',
        controller:'SingleCtrl'
      })
      .otherwise({
        redirectTo: '/'
      })
}]);
afroEarthApp.controller('afroEarthMainCtrl',['$scope','$http','$location','$cookies', '$rootScope', '$route', function($scope, $http, $location, $cookies, $rootScope, $route) {
...some code...
}]);

afroEarthApp.controller('eventsCtrl',['$scope','$http','$location','$cookies', '$rootScope', '$route', function($scope, $http, $location, $cookies, $rootScope, $route) {
...some code...
}]);

afroEarthApp.run(['$log', '$rootScope', '$route', function ($log, $rootScope, $route) {
    $rootScope.$on('$viewContentLoaded', function ($rootScope, $route) {
...some code...
})
}]);

home.html

        <ul class="nav navbar-nav navbar-right">
            <li><a class="page-scroll" href="#websites">Lifestyle sites</a></li>
            <li><a class="page-scroll" href="stories">Success Stories</a></li>
            <li><a href="#/events" data-target="#events" target="_self">Events</a></li>
            <li><a class="page-scroll" ng-href="http://{{singleNiche.url}}.afroearth.com/login/">Login</a></li>
            <li class="other-countries">
                <a href="#">
                    {{singleNiche.country}}<img src="{{singleNiche.countryFlag}}"><i class="icon-caret-down"></i>
                </a>
                <ul>
                    <li ng-repeat="country in singleNiche.countries"><a ng-click="setCountry(country.url)" >{{country.name}}</a></li>
                </ul>
            </li>
        </ul>

Can you hepl me please.

3
  • 4
    run block is executed only once at the start of your app, hence it works on reload. Commented Apr 7, 2016 at 12:58
  • Well, That's the way angular run :) The run function is called only when angular is loaded. Commented Apr 7, 2016 at 13:12
  • Ok. What I should to do? In the app.run code for all controllers. Commented Apr 7, 2016 at 13:18

1 Answer 1

1

add the following in your app.run

 $rootScope.$on('$locationChangeStart', function (event, nextLocation, currentLocation) {
//your code here
});

This function will be call on every url change.

Sign up to request clarification or add additional context in comments.

1 Comment

afroEarthApp.run(['$log', '$rootScope', '$route', function ($log, $rootScope, $route) { $rootScope.$on('$locationChangeStart', function (event, nextLocation, currentLocation) { $rootScope.$on('$viewContentLoaded', function ($rootScope, $route) { I added like this, but still doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.