2016-11-18 164 views
0

我有一個包含不同產品的主頁,我想在其他視圖中顯示一個選定的產品。 (詳細視圖)。顯示我使用ng-repeat的產品,它遍歷product-Array。從ng-repeat中獲取一個項目

現在,當點擊一個按鈕時,我會從ng-repeat中獲得單個產品。然後,視圖會隨着所選產品而變爲細節視圖。視圖的切換應該通過id,因爲這需要向我的API發出請求。

在此狀態下,我嘗試使用$ routeParams,但它並沒有工作。然後我在這裏搜索了stackoverflow。我讀過它可能是範圍問題,因爲我試圖將ID傳遞給控制器​​。 (我需要另一個控制器中的Id)。 當我點擊主頁上的按鈕時,沒有任何事情發生。

我希望,有人有一個想法,如何解決這個問題。

這裏是API的stample JSON文件:

{ 
    "_id": "582ae2beda0fd7f858c109e7", 
    "title": "Lorem ipsum", 
    "smalldescription": "Lorem ipsum dolor sit amet,", 
    "longdescription": "Lorem ipsum dolor sit ametLorem ipsum ", 
    "price": "2500", 
    "img": "Imagetxt" 
} 

這裏是我的代碼:

app.js

angular.module('Productportfolio', ['ngRoute']) 

.config(['$routeProvider', function ($routeProvider) { 

    $routeProvider 
     .when('/home', { 
      templateUrl: '/components/home/home.html', 
      controller: 'ProductCtrl' 
     }) 

     .when('/detail/:productId', { 
      templateUrl: '/components/detail/detail.html', 
      controller: 'DetailCtrl' 

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

home.html的

<div ng-controller="ProductCtrl as ProductCtrl"> 
    <div ng-repeat="product in ProductCtrl.products "> 
     <h3>{{product.title}}</h3> 
      <button ng-click="loadDetail(product)">Detailansicht</button> 
     </div> 
    </div> 

ProductCtrl.js

angular.module('Productportfolio') 

.controller('ProductCtrl', ['APIService','$location', function (APIService,$location) { 

    var vm = this; 
    vm.products = null; 

    init(); 

    function init() { 
     APIService.getProducts() 
      .then(function (response) { 
       vm.products = response.data; 
      }) 
      .catch(function (error) { 
       console.log("error at GET"); 
      }); 
    } 

    vm.loadDetail = function(product){ 
     $location.path('/detail'+product.id); 
    } 
}]); 

APISvc.js

angular.module('Productportfolio') 

.service('APIService', ['$http', function ($http) { 

    var svc = this; 
    svc.root = 'http://localhost:3000/api'; 

    svc.API = { 
     'getProducts': getProducts, 
     'getProduct' : getProduct 
    }; 
    return svc.API; 

    function getProducts() { 
     return $http({ 
      method: 'GET', 
      url: svc.root + '/products' 
     }) 
    } 

    function getProduct(id){ 
     return $http({ 
      method: 'GET', 
      url : svc.root +'/product/'+id 
     }) 
    } 
}]); 

DetailCtrl.js

angular.module('Productportfolio') 

.controller('DetailCtrl', ['APIService', '$routeParams', function (APIService, $routeParams) { 


    var vm = this; 
    vm.product = null; 
    vm.productId = $routeParams.productId; 

    init(); 

    function init() { 
     APIService.getProduct(vm.productId) 
      .then(function (response) { 
       console.log(response); 
       vm.product = response.data; 
      }) 
      .catch(function (error) { 
       console.log("error at GET"); 
      }); 
    } 
}]); 
+0

檢查vm.loadDe中的路徑tail()函數。我認爲它是$ location.path('/ detail /'+ product.id); – neelima

+0

年,你是對的。我改變了它,點擊按鈕時沒有任何反應。我不知道這種方法是否正確地通過ng-click來提交產品 –

+0

您可以嘗試從div中刪除ng控制器嗎? –

回答

0

本該

angular.module('Productportfolio', ['ngRoute']) 

.config(['$routeProvider', function ($routeProvider) { 

    $routeProvider 
     .when('/home', { 
      templateUrl: '/components/home/home.html', 
      controller: 'ProductCtrl', 
      controllerAs: 'ProductCtrl' //modified as your using controller as syntax 
     }) 

     .when('/detail/:productId', { 
      templateUrl: '/components/detail/detail.html', 
      controller: 'DetailCtrl', 
      controllerAs : 'DetailCtrl'  //modify this also    

      //add these lines 
      param:{ 
      productId:null 
      } 
     } 

     .otherwise({redirectTo: '/home'}); 
}]); 
更換你的模塊文件

與此

angular.module('Productportfolio') 

.controller('ProductCtrl', ['APIService','$location', function (APIService,$location) { 

    var vm = this; 
    vm.products = null; 

    init(); 

    function init() { 
     APIService.getProducts() 
      .then(function (response) { 
       vm.products = response.data; 
      }) 
      .catch(function (error) { 
       console.log("error at GET"); 
      }); 
    } 

    vm.loadDetail = function(product){ 
     $location.path('/detail/'+product._id); // you missed a slash and the property was wrong 
    } 
}]); 

這是正確的 $ location.path( '/細節/' + product._id)更換您的ProductCtrl.js;

+0

是什麼‘參數:pruductId:空’是什麼意思? –

+0

@AmeLives更新了帖子。請嘗試新的。如果你沒有得到這個權利,讓我知道! – Aravind

+0

它不起作用。我認爲這是因爲「param:pruductId:null」。我的IDE顯示我的錯誤,因爲它預期的,pruductId ANS空 - >'PARAM: 的productId,null' –

1

從div中刪除ng控制器並像下面那樣更新。

<div > 
    <div ng-repeat="product in vm.products "> 
     <h3>{{product.title}}</h3> 
     <button ng-click="vm.loadDetail(product)">Detailansicht</button> 
    </div> 
</div> 

此外,在航線配置中添加controllerAs:

刪除: '虛擬機'

.when('/home', { 
    templateUrl: '/components/home/home.html', 
    controller: 'ProductCtrl', 
    controllerAs: 'vm' 
}) 

更新像

vm.loadDetail = function(product){ 
    $location.path('/detail'+product['_id']); 
} 
+0

這是否意味着,我不需要NG控制器,因爲它在模塊中定義,對不對? –

+0

它已經在路由配置中指定。 –

+0

好,但鑑於沒有連接到的觀點,因爲我得到了與NG-控制器我得到我錯了,UPPS,我沒用過的ProductCtrl在我的NG-重複右視圖 –

0

編輯 作爲@Paulson彼得說功能ng和div控制器更新如下。

<div > 
    <div ng-repeat="product in vm.products "> 
    <h3>{{product.title}}</h3> 
    <button ng-click="vm.loadDetail(product)">Detailansicht</button> 
</div> 
在路由配置

另外補充controllerAs: '虛擬機'

.when('/home', { 
    templateUrl: '/components/home/home.html', 
    controller: 'ProductCtrl', 
    controllerAs: 'vm' 
}) 

如@亞拉文說:

vm.loadDetail = function(product){ 
    $location.path('/detail/'+product._id); //here is the change 
    } 

現在我可以拿到產品的角度變化

+0

$ location.path('/細節/'+ product._id); //這裏是變化這是由我修復! – Aravind

+0

年,你是對的;對不起 –

+0

我不能將其標記爲一個答案,因爲我是我自己,我必須等待2天直到現在 –