How to call a function on every view change?

2,174 views
Skip to first unread message

Roberto Martínez

unread,
Oct 11, 2012, 3:08:14 PM10/11/12
to ang...@googlegroups.com
I wonder if there is a way to call a function every time the url change?

Pawel Kozlowski

unread,
Oct 11, 2012, 3:16:28 PM10/11/12
to ang...@googlegroups.com
Hi Roberto!

Depends if you are using routes or not. With routes the easiest
(probably) way of approaching things is by listening o the
$routeChangeSuccess event: http://docs.angularjs.org/api/ng.$route

If you don't have routes setup in your app you could also watch
$location.path()
http://docs.angularjs.org/api/ng.$location

Would be easier to help if you could elaborate more on your app and
the use case.

Cheers,
Pawel

On Thu, Oct 11, 2012 at 9:08 PM, Roberto Martínez <rom...@gmail.com> wrote:
> I wonder if there is a way to call a function every time the url change?
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>



--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Nolan Dubeau

unread,
Oct 11, 2012, 3:37:48 PM10/11/12
to ang...@googlegroups.com
Pawel helped me out with the same question yesterday.

Here is what I added to my controller

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

       // console.log(current.params,'current');

        //console.log(previous.params,'previous');      

});


As he mentioned you can also listen for $routeChangeSuccess.

Thanks Pawel!

Nolan

Roberto Martínez

unread,
Oct 11, 2012, 4:02:38 PM10/11/12
to ang...@googlegroups.com
Thanks Pawel

Yes, 'm using routes and it looks like:

angular.module('appTN', ['app.filters', 'app.services', 'app.directives'])
  .
config(['$routeProvider', function ($routeProvider) {
  $routeProvider.when('/dash', {
    templateUrl: 'partials/dash.html',
    controller: DashCtrl
  });
  $routeProvider.when('/perf/sum/:lgaId', {
    templateUrl: 'partials/perf.html',
    controller: PerfCtrl
  });

From the Nolan's comment I'm inferring I need to use this on everyone of my controllers. Is there a way to put this on a global level?

The function I'm calling is a service method. 

Thanks.

Pawel Kozlowski

unread,
Oct 11, 2012, 4:06:46 PM10/11/12
to ang...@googlegroups.com
Hi!

On Thu, Oct 11, 2012 at 10:02 PM, Roberto Martínez <rom...@gmail.com> wrote:
>
> From the Nolan's comment I'm inferring I need to use this on everyone of my
> controllers. Is there a way to put this on a global level?

No, not, at all! It is enough to do it in a top-level controller (the
one above ngView) if it is defined in your app.
Alternatively you could declare an event handler in the .run()
function of a module where you've got access to the $rootScope.

All depends on what you service is doing and what you want to do with
the results of the call to this service.

Cheers,
Pawel

Roberto Martínez

unread,
Oct 11, 2012, 4:13:55 PM10/11/12
to ang...@googlegroups.com
Great! The top-level controller option solved my problem.

Thanks a lot guys.

Edgardo Quiroz

unread,
Nov 13, 2012, 9:24:39 AM11/13/12
to ang...@googlegroups.com
Hi Pawel,

I have a doubt, Do you thing that is possible to prevent that a route changes the url with this event ("$routeChangeStart")?

regards,
Edgardo

Vili

unread,
Nov 27, 2012, 5:05:32 AM11/27/12
to ang...@googlegroups.com
Hi Pawel,

Can you please help me here...
i want to stop $routeChangeStart event, is it possible to cancel this event...?
Reply all
Reply to author
Forward
0 new messages