0

I am trying to build a page with a static header and footer layout, but using ng-view or ng-include to change my container template based on the URL. I've tried two methods:

Using $routeProvider:

app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/', {
        template: 'views/home.html',
        controller: 'Ctrl',
    });
    $routeProvider.when('/contact-us', {
        template: 'views/contact.html',
        controller: 'Ctrl',
    });
    $routeProvider.otherwise({
        redirectTo: '/'
    });

}]);

or a controller function to return a template URL:

app.controller('locationCtrl', function ($scope, $location, $routeParams) {
    $scope.templateUrl = function() {
        return "/views/home.html";

    }
});

However, with either one I get am unable to get a contact-us error message.

2
  • so I have a www.domain.com and this will loads my home.html. and www.domain.com/contact-us will loads my contact.html Commented Sep 18, 2013 at 19:51
  • What happens when user browses w/ JavaScript turned off? Commented Sep 18, 2013 at 20:13

3 Answers 3

2

routerProvider runs before all of controllers. So you can't simply change templateUrls dynamically. But you can use ng-include conditionally:

<div ng-controller="SubPageCtrl>
    <ng-include src="templateUrl"></ng-include>
</div>
<script>
function SubPageCtrl($scope) {
    if ( something ) {
         $scope.templateUrl = '/some.html';
    } else {
         $scope.templateUrl = '/other.html';
    }
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

1
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/', {
    template: 'views/home.html',
    controller: 'Ctrl',
});
$routeProvider.when('/contact-us', {
    template: 'views/contact.html',
    controller: 'Ctrl', // Controller should be different
});
$routeProvider.otherwise({
    redirectTo: '/'
});
}]);

Replace To :

 app.config(['$routeProvider', function ($routeProvider) 
$routeProvider.when('/', {
    templateUrl: 'views/home.html', //template should be templateUrl
    controller: 'Ctrl',
});
$routeProvider.when('/contact-us', {
    templateUrl: 'views/contact.html', //template should be templateUrl
    controller: 'Ctrl',
});
$routeProvider.otherwise({
    redirectTo: '/'
});
}]);

Your 1st method will work.

7 Comments

Thanks It works on home.html but when in contact-us, seems it keep reloading the page.
I'd recommend not recycling controllers for fundamentally separate routes. You wouldn't do this with MVC so it's not advisable with Angular despite being possible.
yeah I did, but it just keep reloading. Sorry I have no idea why its happening.
$routeProvider.when('/contact-us', { templateUrl: 'views/contact.html', }); Here include Controller jsfiddle.net/NYgUT/3
Yeah I've tried that, but still the index page keep rendering ( reloading? )
|
0

You should define your routes from more specific urls to more general urls. Because routes match with urls that start with your definition.

app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/contact-us', {
        template: 'views/contact.html',
        controller: 'Ctrl',
    });
    $routeProvider.when('/', {
        template: 'views/home.html',
        controller: 'Ctrl',
    });
    $routeProvider.otherwise({
        redirectTo: '/'
    });

}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.