Controller initialization logic does not hit when user returns

So, I use angular to make a web page and inside I have tabs that set ng-view in the main part of my page. Every time a page loads a different view, I want to run some logic to adjust the situation. The strange problem I am facing is that it works fine when the user loads a web page or refreshes, but if they go to one tab and then click the back button in the browser, new ng-views are loaded, but the controller logic is not called again. I know that the controller is loading, but the logic in the root of the controller does not start. I tried to add an hour frame for stateChangeSuccess and viewContentLoaded, none of which start when the user clicks back.

CONTROLLER

 myApp.controller('RootCtrl',  function($scope, $location) {



$scope.$watch('viewContentLoaded', function(){
    Console.log("this is only hit once also");
});

Console.log("this is only hit once");
});

html

APP

myApp = angular.module('myApp', ['ngRoute','ui.bootstrap']);

myApp.config([
'$routeProvider','$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider


    .when('/home',{
      templateUrl: '../templates/home.html',
      controller: 'other'
    })

    .when('/myOtherPage',{
        templateUrl: '../templates/page1.html',
        controller: 'otherCtrl',
        css: 'royaltyOverrides.css'
    })

    .when('/myOtherPage2',{
        templateUrl: '../templates/page2.html',
        controller: 'otherCtrl',
    })
    .otherwise({redirectTo:"/home"}
    );

    $locationProvider.html5Mode(true);
}
]);
+4
1

:

  • 'RootCtrl' , ngController
  • $scope.$watch() , viewContentLoaded , .
  • $viewContentLoaded - , $on, DOM .

RootCtrl:

$scope.$on('$viewContentLoaded', function(event) {
    // you logic code goes here
});
0

Source: https://habr.com/ru/post/1584552/


All Articles