javascript - Controller initialization logic not hit when user goes back -
so using angular make webpage , inside have tabs set ng-view in main part of page. every time page loads different view want run logic set things up. weird issue running works fine when user loads webpage or refreshes if go 1 tab , press button on browser, new ng-view loads controller logic not called again. know controller being loaded, logic in root of controller isn't being run. have tried adding scope watch statechangesuccess , viewcontentloaded, neither of run when user presses back.
controller
myapp.controller('rootctrl', function($scope, $location) { $scope.$watch('viewcontentloaded', function(){ console.log("this hit once also"); }); console.log("this hit once"); }); this injected top of html body , in every tab
app setup
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); } ]);
let's clarify concepts:
'rootctrl'parent controller loaded once calledngcontrollerdirective$scope.$watch()watches scope variable ,viewcontentloadednot 1 of them shown in code.$viewcontentloadedemitted event can listen$onmethod , emitted every time change view , view loaded it's dom , directives.
please try following code @ rootctrl controller:
$scope.$on('$viewcontentloaded', function(event) { // logic code goes here });
Comments
Post a Comment