简体   繁体   English

无法读取angularjs中未定义错误的属性“注释”

[英]Cannot read property 'comments' of undefined error in angularjs

I asked this question before too. 我也问过这个问题。 Maybe someone can caught the error here. 也许有人可以在这里捕获错误。

.controller('DishDetailController', ['$scope', function($scope) {
    var dish={
        name:'Uthapizza',
        image: 'images/uthapizza.png',
        category: 'mains', 
        label:'Hot',
        price:'4.99',
        description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
        comments: [
            {
                rating:5,
                comment:"Imagine all the eatables, living in conFusion!",
                author:"John Lemon",
                date:"2012-10-16T17:57:28.556094Z"
            },
            {
                rating:2,
                comment:"It's your birthday, we're gonna party!",
                author:"25 Cent",
                date:"2011-12-02T17:57:28.556094Z"
            }
        ]
    };
    $scope.dish = dish;
}])
.controller('DishCommentController', ['$scope', function($scope) {
    $scope.newcomment = {
        rating : "",
        comment: "",
        author: "",
        date: new Date().toISOString()
    };
    $scope.submitComment = function () {
        $scope.newcomment.date = new Date().toISOString();
        $scope.newcomment.rating = parseInt($scope.newcomment.rating)
        console.log($scope.newcomment.comment);
        console.log($scope.newcomment.author);
        console.log($scope.newcomment.rating);
        console.log($scope.newcomment.date);
        $scope.dish.comments.push($scope.newcomment);
    }
}]);

In html part part I have only two-three textbox. 在html部分中,我只有两三个文本框。 SubmitComment in button onclick. 在按钮onclick上的SubmitComment Console.log show me the result which I need. Console.log向我显示所需的结果。 But I cannot push this to array. 但是我无法将其推送到数组。 The exercise is a part of tutorial. 该练习是教程的一部分。 So it was requirement to write them in seperate controllers and this was provided by the tutorial 因此,需要将它们编写在单独的控制器中,并且由教程提供

The dish object is not defined in the second controller which make sense from the error "undefined". 碟形对象未在第二个控制器中定义,这从错误“未定义”中可以理解。 You have to find a way to pass the dish object from the first controller "DishDetailController" to the second one "DishCommentController". 您必须找到一种方法,将碟形对象从第一个控制器“ DishDetailController”传递到第二个“ DishCommentController”。 There are several ways to do it, one way is to stock the object into service from the first controller then retrieve it from the same service in the second controller. 有几种方法可以做到这一点,一种方法是将对象存储到第一个控制器的服务中,然后从第二个控制器的同一服务中检索它。 As I know the best way to share objects between controlllers is via services. 据我所知,在控制器之间共享对象的最佳方法是通过服务。

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

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