Manish Manish - 1 month ago 19
AngularJS Question

ui-router angular master detail view

I'm trying to get the userlist [master] and user details[detail] on the same page.

https://plnkr.co/edit/EKY2pztGkKXUuQIyefUY?p=preview

module and configuration.

var myModule = angular.module("myApp", ['ui.router']);

myModule.config(function($stateProvider, $urlRouterProvider) {

$stateProvider
.state('user', {
url : '/users',
templateUrl:'users.html',
controller : 'usersController'
})
.state('user.details', {
url : '/:id',
templateUrl:'users.html',
controller : 'usersControllerDetail'
});

$urlRouterProvider.otherwise("/users");

});


myModule.controller('usersController' , function($scope){
$scope.users = [{username : 'manish', id : 1}, { username : 'kaustubh', id :2} ]
});

myModule.controller('usersControllerDetail' , function($scope, $stateParams){
console.log($stateParams.id);
});


when ever I click on the user in the master list , master list is rendered twice on scree.

can you please let me know where I'm going wrong

Answer

There is adjusted and working plunker

Just use different template for a child

.state('user.details', {
      url : '/:id',
      //templateUrl:'users.html',
      templateUrl:'details.html',
      controller : 'usersControllerDetail'
});

this could be the template

<div>

 <div ui-view>
  <h3>current state name: <var>{{$state.current.name}}</var></h3>


  <h5>params</h5>
  <pre>{{$stateParams | json}}</pre>
  <h5>state</h5>
  <pre>{{$state.current | json}}</pre>

 </div>
</div>