UI-Router $stateParams With Master Detail Pattern

290 views Asked by At

I need a Master Detais pattern for my web site. In index lots of thumbnails and every thumbnail link to their detailed page. I make a progress but does not get to work. Its load thumbnails on index oage but when I click to thumbnails its load blank page.

Index:

<body ui-view="viewA">
<div class="row" ui-view="viewB">
        <div class="col s12 m6 l4" ng-repeat = "manga in mangas">
        <div class="row">
        <div class="col s5">
            <a ui-sref="icerikDetails({id:manga.id})" class="thumbnail">
            <img src="/kapaklar/{{manga.kapak}}">
</a>

JS:

angular.module('nasuh',["ui.router"])
.config(function($stateProvider, $urlRouterProvider) {

$stateProvider
.state('index', {
   url: "/",
   views: {
   "viewA": { templateUrl: "index.html",
            controller: "indexCtrl",}
}})
$stateProvider
.state('icerik', {
   url: "/icerik",
   views: {
        "viewB": { templateUrl: "icerik.html",
                   controller: "mmgCtrl",},
}})
$stateProvider
.state('icerikDetails', {
    url: "/icerik/:id",
    template: "<div>{{details }}</div>",
    controller: "mmgCtrl",
    resolve: {
             getData : function(MY, $stateParams, $filter){
             return MY.isimler.then(function(res){
             return $filter('filter')(res.data, {id: $stateParams.id}, true)[0];
});}
}
})
$urlRouterProvider.otherwise("/");
})

factory and controllers:

.factory('MY', function($http){
 var factory = {};
 var url = '/uzak/remote.php?callback=JSON_CALLBACK';
 factory.isimler = $http.get(url);
 return factory;
})
.controller('indexCtrl', function($scope, MY) {
     MY.isimler.success(function(alHemen){
          $scope.mangas = alHemen;
     });  
 })
.controller('mmgCtrl', function($scope, getData) {
 $scope.manga = getData.then(function(data){
   $scope.details = data;
 });})
1

There are 1 answers

1
Radim Köhler On BEST ANSWER

I am not sure what you are trying to achieve, but at least I tried to convert your snippets into something working.

There is a working example

To get more understanding, do read these:

Nested States and Nested Views

Multiple Named Views

These are states, with a hiererchy. Index is super root, and details is super child:

$stateProvider
    .state('index', {
      url: "/",
      views: {
        "": {
          templateUrl: "index.tpl.html",
          controller: "indexCtrl",
        }
      }
    })
    .state('icerik', {
      parent: "index",
      url: "^/icerik",
      views: {
        "viewA": {
          templateUrl: "icerik.html",
          controller: "indexCtrl",
        },
      }
    })
    .state('icerik.icerikDetails', {
      url: "/icerik/:id",
      template: "<div>{{manga}}</div>",
      controller: "mmgCtrl",
      resolve: {
        getData: function(MY, $stateParams, $filter) {
          return MY.isimler.then(function(res) {
            return $filter('filter')(res.data, {
              id: $stateParams.id
            }, true)[0];
          });
        }
      }
    })
  $urlRouterProvider.otherwise("/");

These will be new controllers and factory:

.factory('MY', function($http) {
    var factory = {};
    //var url = '/uzak/remote.php?callback=JSON_CALLBACK';
    var url = 'data.json';
    factory.isimler = $http.get(url);
    return factory;
})
.controller('indexCtrl', function($scope, MY) {
    MY.isimler.success(function(alHemen) {
      $scope.mangas = alHemen;
    });
})
.controller('mmgCtrl', function($scope, getData) {
    //$scope.manga = getData.then(function(data){
    //   $scope.details = data;
    // });
    $scope.manga = getData;
})

And an example of templates "index.tpl.html":

<div >
  <h2>  this is index view</h2>
  <a href="#/icerik">icerik</a>

    <div ui-view="viewA"></div>    
</div>

and "icerik.html":

<div class="row" >
        <div class="col s12 m6 l4" ng-repeat="manga in mangas">
        <div class="row">
        <div class="col s5">
            <a ui-sref="icerik.icerikDetails({id:manga.id})" class="thumbnail">
            img src="/kapaklar/{{manga.kapak}}" 
            </a>
  </div>
  </div>
  </div>

  <hr>
  <div ui-view=""></div> 
</div>

Check it here