Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS view doesn't change when navigating to link

Tags:

angularjs

I'm baffled by the following: I have a simple link like this:

    <li><a href="#/foo">Foos</a></li>

but when clicking on it, the view is not updated. The URL in the browser changes, but nothing happens, and nothing gets displayed in the console. If I load the URL directly in the browser, then the correct page is loaded.

The routes look like this:

app.config(function($routeProvider, RestangularProvider) {
    $routeProvider
        .when('/index', {templateUrl: 'assets/views/index.html'})
        .when('/foos', {templateUrl: 'assets/views/list.html', controller: controllers.FooListCtrl})
        .when('/foos/create', {templateUrl: 'assets/views/update.html', controller: controllers.FooUpdateCtrl})
        .when('/foos/:id/update', {templateUrl: 'assets/views/update.html', controller: controllers.FooUpdateCtrl})
        .otherwise({redirectTo: '/index'});

    RestangularProvider.setBaseUrl("/admin");
});
like image 835
Manuel Bernhardt Avatar asked Jul 24 '13 13:07

Manuel Bernhardt


2 Answers

Ok, so apparently, this is a known issue of sorts: when the locationProvider (i.e. $location) is injected to one controller, links cease to work globally. One workaround seems to be to rewrite the links via jQuery, and that works:

app.run(function($rootScope) {
    $('[ng-app]').on('click', 'a', function() {
        window.location.href = $(this).attr('href');
    });
});
like image 145
Manuel Bernhardt Avatar answered Oct 15 '22 17:10

Manuel Bernhardt


I have been able to resolve this issue by adding target="_self" to the link.

<li><a href="#/foo" target="_self">Foos</a></li>

I found this resolution here.

like image 31
snumpy Avatar answered Oct 15 '22 17:10

snumpy