Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Controller\View lifecycle events #1830

Closed
wiistriker opened this issue Jan 18, 2013 · 3 comments
Closed

Controller\View lifecycle events #1830

wiistriker opened this issue Jan 18, 2013 · 3 comments

Comments

@wiistriker
Copy link

I have simple app and on one page i need create WebSocket connection when page is rendered. When i leave page i need close this connection. So i basically need simpliest lifecycle events to do this. So, i try:

<body>
    <ul class="nav">
        <li><a href="#donate">Donate</a></li>
         <li><a href="#about">About</a></li>
     </ul>
    <div class="container" ng-view></div>
</body>
angular.module('project', []).
config(function($routeProvider) {
    $routeProvider.
        when('/', {controller:HomeCtrl, templateUrl:'templates/home.html'}).
        when('/about', {controller:AboutCtrl, templateUrl:'templates/about.html'}).
        when('/donate', {controller:DonateCtrl, templateUrl:'templates/donate.html'}).
        otherwise({redirectTo:'/'});
});

function HomeCtrl($scope, $routeParams, $http) {
}

function DonateCtrl($scope) {
    $scope.$on('$viewContentLoaded', function(event) {
        console.log('viewContentLoaded');
    });

    $scope.$on('$destroy', function() {
        console.log('destroy');
    });
}

function AboutCtrl($scope) {
}

when i load DonateCtrl, and leave them (for example, to #about page) i see in my console:

viewContentLoaded
destroy

Everything is good.

but if i go back to #donate url i get this:

viewContentLoaded
destroy
viewContentLoaded

Why viewContentLoaded called twice? And why destroy even call??

And there is another related issue: if i click on #donate url again and again angular.js will update content with my template again and again and trigger viewContentLoaded event, just like this controller+view loaded for the first time. Can i prevent this bahaviour and prevent content loading if i request same url?

@wiistriker
Copy link
Author

AngularJS v1.0.3

@wiistriker
Copy link
Author

After some investigation i found out: that behaviour depends on

scope.$on('$routeChangeSuccess', update);

Now try figure out why this event triggered twice

@wiistriker
Copy link
Author

Got it! I should create urls like #/donate but i have #donate

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant