Useer
Useer

Reputation: 61

ui-router ui-view not updating info based on main view

I have a main page displaying a list of houses and when I click on a particular house I want to display the house details. I am trying to achieve this using ui-view however, the house details are not showing.

These are my routes defined:

var app = angular.module('app', ['ui.router']);

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

    $urlRouterProvider.otherwise('/about');

    $stateProvider

        .state('home', {
            url: '/home',
            templateUrl: 'app/views/partial-home.html'
        })

        .state('about', {
            url: '/about',
            templateUrl: 'app/views/partial-about.html',
            controller: 'inventoryCtrl'     
        })

        .state('about.item', {
            url: '/:id',
            templateUrl: 'app/views/partial-about-item.html',
            controller: 'detailsCtrl'
        });      
    });

this is my main page html displaying a list of all houses:

 <div ng-repeat="value in all | filter: (!!locationFilter || undefined) && {type: locationFilter} | filter: priceFilter | filter: datesFilter | orderBy:sortProp">
                  <ul>
                     <li><img src="app/images/{{value.image}}.jpg" alt="Smiley face" height="100" width="240"></li>
                     <li><strong>{{value.id}}</strong></li>
                     <li><strong>{{value.address}}</strong></li>
                     <li>city: {{value.city}}</li>
                     <li>postcode: {{value.postcode}}</li>
                     <li>price: £{{value.price}}</li>
                     <li>num_of_beds: {{value.num_of_beds}}</li>
                     <li>{{value.type}}</li>
                     <li>{{value.minutes}}</li>
                     <li>{{value.added}}</li>
                  </ul>
                   <a ng-href="#/about/{{value.address}}" class="btn btn-primary">View details</a> 
               </div>
<div ui-view></div>

this is my house details html displaying the house details which is only displaying the static text nothing inside ng-repeat or curly braces is shown

updated html:

<div>
<ul ng-repeat="item in singleHouse track by item.id">
 <li>{{item.id}}</li>
 <li>{{item.desc}}</li>
</ul>
</div>

this is my page details controller:

app.controller('detailsCtrl', ['$scope', 'DetailsFactory', '$stateParams', '$state', function($scope, DetailsFactory, $stateParams, $state) {

      $scope.id = $stateParams.id;

        DetailsFactory.getHouseDetails($stateParams.id).then(function(response){
          $scope.singleHouse = response.detailsData.details;
          console.log($scope.singleHouse);
        })

    }]);

I have added factory as well for the details house page:

app.factory('DetailsFactory', ['$http', '$stateParams', function($http, $stateParams) {

      var urlBase = 'app/default/details.json';
    var factory = {};

    factory.getHouseDetails = function(id){
        return $http.get(urlBase + id); 
    }
    return factory;

}]);

and the json for the details page:

{

   "detailsData":{

        "details": [
            {
            "id": 1,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 2,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 3,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 4,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 5,
            "desc": "Beautiful house blebel eble"  
        }, {
            "id": 6,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 7,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 8,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 9,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 10,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 11,
            "desc": "Beautiful house blebel eble"
        }, {
            "id": 12,
            "desc": "Beautiful house blebel eble"
        }]
    }
}  

Upvotes: 0

Views: 64

Answers (2)

$stateParams.item just gives id. In your details controller you should get the house detail from server

app.controller('detailsCtrl', ['$scope', 'InventoryFactory', '$stateParams', '$state', function($scope, InventoryFactory, $stateParams, $state) {

    InventoryFactory.getHouseDetail($stateParams.item).then(function(response){
      $scope.singleHouse  = response.data;
    })
}]);

In your html

<ul ng-repeat="item in singleHouse">
 <li>{{item.id}}</li>
 <li>{{item.desc}}</li>
</ul>

Upvotes: 2

Cameron Bruce
Cameron Bruce

Reputation: 9

Your ng-href="#/about/{{value.address}}" doesn't match up with your state declaration url: '/:item', it should be url: 'about/:item',

Upvotes: -1

Related Questions