简体   繁体   English

AngularJS和routeParams

[英]Angularjs and routeParams

I have a piece of code that is getting JSON data from a local server. 我有一段代码从本地服务器获取JSON数据。 I am trying to display those data into a page and then when i'll click on a product i want to see the details of it. 我试图将这些数据显示在页面中,然后当我单击某个产品时,我想查看它的详细信息。 So far i can display all my products into a page but when i'm heading to the details page i got an error Cannot read property 'id' of undefined. 到目前为止,我可以将我所有的产品显示在页面中,但是当我转到详细信息页面时,出现错误无法读取未定义的属性“ id”。 Can you please help me out? 你能帮我吗?

This is my code so far. 到目前为止,这是我的代码。

app.js app.js

'use strict';

angular.module('app', []).
config(function($routeProvider){
    $routeProvider.
        when("/", {templateUrl: "/partials/products.html"}).
        when("/details/:id", {templateUrl: "/partials/details.html", controller: DetailsCtrl}).
        otherwise({redirectTo: "/"});
})


function AppCtrl($scope, $http){
$http.get('http://localhost:3000/api/products/727617361726372')
    .success(function(data){
        $scope.productList = data;

        /*$scope.filterProductsByCategory = function(category){
            $scope.search = category
        };*/
    })
    .error(function(data){
        console.log("Error!: ");
    });
}

function DetailsCtrl($scope, $routeParams){
   $scope.item = $scope.productList[$routeParams.id];
}

details.html details.html

<h2>This is the details pages</h2>
product id: {{ item.product_id }}
<br/>
product name: {{ item.product_name}}

index.html index.html

<div class="container" ng-app="app" ng-controller="AppCtrl">
    <h1 class="text-danger">Welcome to our Store</h1>
    <hr/>
    <ng-view></ng-view>
</div>

products.html products.html

<div class="col-lg-4 col-sm-6 col-xs-12" ng-repeat="product in productList">
<div class="thumbnail">
    <img src="img/img3.jpeg">
    <div class="caption">
        <h4><a href="#/details/{{product.product_id}}">{{ product.product_name }}</a></h3>
        <p>Category: {{ product.category_name }} ({{ product.category_id }})</p>
        <p class="text-muted">{{ product.product_details}}</p>
        <ul class="list-inline">
            <li>
                <p class="product-price btn">&euro; {{ product.product_price }}</p>
            </li>
            <li>
                <a href="#" class="btn btn-success">Add to Cart</a>
            </li>
        </ul>
    </div>
</div>

Thanks!!! 谢谢!!!

you are getting the productList in your AppCtrl . 您将在productList中获得AppCtrl If you want to access a specific item in your DetailsCtrl you'll need to get the specific item since you can't access AppCtrl scope variables in another Ctrl. 如果你想在你访问一个特定项目DetailsCtrl你需要得到具体的项目,因为你不能访问AppCtrl在另一按Ctrl范围内的变量。

The best way to do this would be to create an extra rest call for getting a single item. 最好的方法是创建一个额外的休息电话以获取单个物品。

A quick way to fix your code, although I really don't recommend it would be to use $rootcope in your AppCtrl 修复代码的快速方法,尽管我真的建议这样 ,但应在$rootcope中使用$rootcope AppCtrl

I'd suggest reading Angular Docs on $scope. 我建议在$ scope上阅读Angular Docs。 It might give you a better understanding how scopes work in angular: https://docs.angularjs.org/guide/scope# ! 它可以使您更好地了解范围如何在角度工作: https : //docs.angularjs.org/guide/scope#

I assume you have injected ngRoute correctly. 我假设您已正确注入ngRoute。

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

Other than that there is nothing wrong with your code. 除此之外,您的代码没有任何问题。

Demo 演示版

Just check your code again. 只需再次检查您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM