Here is one way to handle URLs with and without id using the standard routeProvider setting, with one controller:
JS:
var app = angular.module('plunker', []); app.config(function($routeProvider){ return $routeProvider .when('/', { controller: 'HomeController', templateUrl: 'home.html' }) .when('/:id', { controller: 'HomeController', templateUrl: 'home.html' }) .otherwise({ redirectTo: '/' }); }); app.controller('HomeController', [ '$scope', '$routeParams', function($scope, $routeParams) { if($routeParams.id){ $scope.id = $routeParams.id;
Plunker
And in a different way, without using ng-view and relying on the $ location service:
var app = angular.module('plunker', []); app.config( ['$locationProvider', function($locationProvider) { $locationProvider.html5Mode(true); } ] ); app.controller('HomeController', [ '$scope', '$location', function($scope, $location) { $scope.$watch(function(){ return $location.hash(); }, function(id){ $scope.id = id; } ); $scope.$watch('id', function(id){ if(id){
Plunker
source share