ui-router nested views example

In this example below you will see how to do a ui-router nested views example with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by -kaik-, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright -kaik- ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>ui-router nested views example</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <html ng-app="myApp">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <!-- get bootstrap CSS -->
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  <!-- get AngularJS -->  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.min.js"></script>
  <!-- get Angular ui-router JS -->    
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.min.js"></script>  
</head>

  
  
<body ng-controller="Ctrl" class="padding">
  <button type="button" class="btn btn-primary" ng-click="navigate()">Navigate HOME</button>
  <button type="button" class="btn btn-primary" ui-sref="home.profile({username:'Bob'})">Navigate to PROFILE</button> 
  <ui-view></ui-view>
</body>
  
  
<script type="text/ng-template" id="views/home.html">
  <ui-view></ui-view>
  <div>{{homeMsg}}</div>
</script>
  
<script type="text/ng-template" id="views/profile.html">
  <div class="bg-info padding">{{user.name}} profile: {{user.level}} level</div>
</script>
  
</html>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/-kaik-/ui-router-nested-views-example-zryMrd */
.padding {
  padding: 30px;
}

.btn {
  margin-bottom: 10px !important;
}


/*Downloaded from https://www.codeseek.co/-kaik-/ui-router-nested-views-example-zryMrd */
angular.module('myApp', ['ui.router'])

.controller('Ctrl', function($scope, $state){
  $scope.navigate = function(){
    $state.go('home', {username: "Marqués"});
  }
})

.controller('HomeCtrl', function($scope, $state, $stateParams){
  $scope.homeMsg = "Home, sweet home " + $stateParams.username;
})

.controller('ProfileCtrl', function($scope, $state, $stateParams){
  $scope.user = {
    name:   $stateParams.username,
    level:  $state.current.data.level
  };
  
})

.config(function($stateProvider){
		$stateProvider
			.state('home', {
				url: '/home/:username',
				templateUrl: 'views/home.html',
        controller: 'HomeCtrl',
        data:{
          level: 'PRO'
        }
			})
    
			.state('home.profile', {
				url: '/profile',
				templateUrl: 'views/profile.html',
        controller: 'ProfileCtrl'
			})    
	});

Comments