Brk Brk - 1 month ago 12
Javascript Question

ui-router nested view doesn't get loaded properly

Hey I have a simple app which I have created.

The app has the following views

1.Settings - Main View.

2.Profile - Sub view, child of Settings View.

3.Account - Sub View, child of Settings View.

I want the application to start from profile page, therefore I wrote the following code:

$urlRouterProvider.otherwise('/profile');


But apparently noting is happen, I got a blank screen and can't see the view properly(settings.profile).

I have added my code to a plunker to make things more convenient and accessible for other developers which can help me solve my problem.
https://plnkr.co/edit/RFP5dUNV876cy8vRDuvL?p=preview

Main main code is like this:

/**
* Module
*
* Description
*/
var app = angular.module('app', ['ui.router'])

app.controller('mainCtrl', ['$scope', function($scope){
$scope.title="Index Page";
}]);

app.controller('ProfileController', ['$scope', function($scope){
$scope.title="Profile Page";
}]);

app.controller('AccountController', ['$scope', function($scope){
$scope.title="Account Page";
}]);

app.controller('SettingsController', ['$scope', function($scope){
$scope.title="Settings Page";
}]);

app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('settings', {
url: '/settings',
templateUrl: 'settings.html',
controller: 'SettingsController'
})
.state('settings.profile', {
url: '/profile',
templateUrl: 'profile.html',
controller: 'ProfileController'
})
.state('settings.account', {
url: '/account',
templateUrl: 'account.html',
controller: 'AccountController'
});
$urlRouterProvider.otherwise('/profile');
});

Answer

Change the .otherwise function to:

$urlRouterProvider.otherwise('/settings/profile');

Since the other routes are nested within settings state, you can only access the nested views via the <ui-view> in settings.html. The urls are also nested within each other if url parameter is defined in both, so /settings/profile is required.

Updated Plnkr

Comments