2016-07-04 130 views
0

我正在用angularjs構建在線商店,我很難弄清楚如何鏈接到details.html並讓它顯示具體的項目詳細信息。目前,當我點擊查看更多按鈕時,網址會顯示該項目的名稱。唯一可以讓它顯示的方法是製作如下鏈接:#/ details /:{{item.name}}。我在網上閱讀的所有內容都表示它應該是#/ details /:name,但是它會返回:名稱在url中,而不是詳細信息/:以Macbook Pro爲例。如果我按照正確的方式將它重定向回主頁。這是我的代碼到目前爲止。

app.js

var myApp = angular.module('app', ['ngRoute']); 
myApp.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) { 
     $routeProvider. 
     when('/home',{ 
      templateUrl: 'partials/home.html', 
      controller: "storeCtrl" 
     }). 
     when('/about',{ 
      templateUrl: 'partials/about.html', 
      controller: "storeCtrl" 
     }). 
     when('/computers',{ 
      templateUrl: 'partials/computers.html', 
      controller: "storeCtrl" 
     }). 
     when('/smartphones',{ 
      templateUrl: 'partials/smartphones.html', 
      controller: "storeCtrl" 
     }). 
     when('/tablets',{ 
      templateUrl: 'partials/tablets.html', 
      controller: "storeCtrl" 
     }). 
     when('/details/:{{item.name}}',{ 
      templateUrl: 'partials/details.html', 
      controller: "storeCtrl" 
     }). 

     otherwise({ 
      redirectTo: '/home'    
     }); 
    }]); 

myApp.controller('storeCtrl', ['$scope', '$http', '$routeParams', function($scope, $http, $routeParams) { 


$scope.homeHeading = 'All Products'; 
$scope.aboutHeading = 'About Us'; 
$scope.computersHeading = 'Computers'; 
$scope.smartphonesHeading = 'Smartphones'; 
$scope.tabletsHeading = 'Tablets'; 

$http.get('js/products.json').then(function(result) { 
    $scope.products = result.data; 


}); 

$scope.minusOne = function(index) { 
    $scope.products[index].dislikes += 1; 
}; 
$scope.plusOne = function(index) { 
    $scope.products[index].likes += 1; 
}; 





}]); 

computers.html

<div> 
<h1>{{computersHeading}}</h1> 

<div class="product col-md-4" ng-repeat="item in products | filter:search | filter:'Laptop'"> 
<img ng-src="{{ item.cover }}" > 
<h3>{{ item.name }}</h3> 
<p class="price">{{ item.price }}</p> 
<p class=""> <b>Storage</b>: {{ item.specs.storage }}</p> 
<p> <b>Memory</b>: {{ item.specs.memory }}</p> 

<!----- Save ratings for details page 
<div class="rating"> 
    <p class="likes" ng-click="plusOne($index)"> 
    <i class="fa fa-thumbs-up"></i> 
    {{ item.likes }} </p> 
    <p class="dislikes" ng-click="minusOne($index)"><i class="fa fa-thumbs-down"></i> {{ item.dislikes }} </p> 

</div> --> 
<a href="#/details/:{{item.name}}" class="btn btn-custom"> View Product</a> 

</div> 

</div> 

再次獲得該項目的名稱在URL中顯示的唯一方法是在url中括號,我知道,是不正確的。任何幫助將不勝感激。

回答

0

我想通了,我需要做一個控制器的細節頁面,然後得到json數據。然後,我將我的href鏈接更改爲

<a href="#/details/{{products.indexOf(item)}}" class="btn btn-custom"> View Product</a> 
相關問題