繁体   English   中英

将对象推送到无法在Angular JS中工作的数组

[英]push object to an array not working in angular js

我想将一个对象推送到数组。 但是它没有发生。 我在提交单击时将对象注释(表单的值)推送到dishDetailController的dish.comments。 ng-controller =“ DishCommentController”嵌套在ng-controller =“ dishDetailController”里面,我哪里出错了?

main.html中

    <div class="row row-content" ng-controller="dishDetailController as menuCtrl">
            <div class="col-xs-12">
                <ul class="media-list">
                <li class="media">
                    <div class="media-left media-top">
                        <a href="#">
                        <img class="media-object img-thumbnail"
                         ng-src={{menuCtrl.dish.image}} alt="Uthappizza">
                        </a>
                    </div>
                    <div class="media-body">
                        <h2 class="media-heading">{{menuCtrl.dish.name}}
                         <span class="label label-danger">{{menuCtrl.dish.label}}</span>
                         <span class="badge">{{menuCtrl.dish.price | currency}}</span></h2>
                        <p>{{menuCtrl.dish.description}}</p>


                    </div>
                </li>

                {{menuCtrl.dishes}}
            </ul>

            </div>
            <div class="col-xs-9 col-xs-offset-1">
                  <div class="media-right"><h3 class="media-right">Customer Comments  &nbsp; &nbsp; &nbsp;<small>Sort By:
                         <input type="text" ng-model="sort"></small></h3></div><br>
                <ul ng-repeat="comments in menuCtrl.dish.comments | orderBy:sort">
                <blockquote>
                <div class="media-body">

                        <h3 class="media-heading">{{comments.rating}} Stars</h3>

                        <h4 class="media-heading"></h4>

                         <p>{{comments.comment }}</p>
  <footer>{{comments.author}},<cite title="Source Title">{{comments.date | date:'MMM,dd,yyyy'}}</cite></footer>


                        <p><li></li></p>


                    </div>
                    </blockquote>
                </ul>

            </div>








         <div class="col-xs-9 col-xs-offset-1" ng-controller="DishCommentController as comment">

                <blockquote>
                <div class="media-body">

                        <h3 class="media-heading">{{star}} Stars</h3>

                        <h4 class="media-heading"></h4>

                         <p>{{comm }}</p>
  <footer>{{name}},<cite title="Source Title">{{}}</cite></footer>


                        <p><li></li></p>


                    </div>
                    </blockquote>

                    <ul class="list-unstyled">


                    </ul>
                <form class="form-horizontal" name="commentForm"
                        ng-submit="submitComment()" novalidate>
                        <p><label>Your Name </label>
                        <input type="TextField" name="name" ng-model="name" style="width:600px;"></p>
                        <p><label>Number of Stars </label><span class="radio_star" ng-init="star=5">
                         <input type="radio" ng-model="star" value="1">1 <input type="radio" ng-model="star" value="2">2 <input type="radio" ng-model="star" value="3">3 <input  type="radio" ng-model="star" value="4">4 <input  type="radio" ng-model="star" value="5"  checked> 5 </span>

                        </p>
                        <p><label style="vertical-align:top;">Your Comments </label>
                         <textarea ng-model="comm" rows="4" cols="70"></textarea> </p>
                         <p>
                            <input type="button" ng-click="submitComment()" value="Comment" />
                         </p>



                </form>
            </div>

app.js

 'use strict';
        angular.module('confusionApp',[])

        .controller('dishDetailController', ['$scope', function($scope)  {

            var order="";
            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:4,
                                   comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
                                   author:"Paul McVites",
                                   date:"2014-09-05T17:57:28.556094Z"
                               },
                               {
                                   rating:3,
                                   comment:"Eat it, just eat it!",
                                   author:"Michael Jaikishan",
                                   date:"2015-02-13T17:57:28.556094Z"
                               },
                               {
                                   rating:4,
                                   comment:"Ultimate, Reaching for the stars!",
                                   author:"Ringo Starry",
                                   date:"2013-12-02T17:57:28.556094Z"
                               },
                               {
                                   rating:2,
                                   comment:"It's your birthday, we're gonna party!",
                                   author:"25 Cent",
                                   date:"2011-12-02T17:57:28.556094Z"
                               }

                           ]
                    };

            this.dish = dish;

        }])



        .controller('DishCommentController', ['$scope', function($scope) {

            //Step 1: Create a JavaScript object to hold the comment from the form


            $scope.submitComment = function () {

                var d = new Date();
                var n = d.toISOString();

                alert($scope.star);

                var comment={
                comment:$scope.comm,
                rating:$scope.star, 
                author:$scope.name,
                date:new Date().toISOString()};
                alert('toto');
                //Step 2: This is how you record the date
              //  "The date property of your JavaScript object holding the comment" = new Date().toISOString();
                alert(comment);
                // Step 3: Push your comment into the dish's comment array
                $scope.dish.comments.push(comment);

                alert($scope.name);
                //Step 4: reset your form to pristine

                //Step 5: reset your JavaScript object that holds your comment
            };
        }])


;   

如果我没记错的话,那么您将从未创建变量$scope.dish ,而仅创建变量var dish = ... ,因此您无法分配任何内容;-)

在寻找有关清除数组的javascript示例时,发生了这种情况。 该问题来自“ Angular JS Coursera课程”的作业。

如果正确使用$ scope,则在初始化ng-controller时无需使用“ as MenuCtrl”。 本课程涵盖了这一点,我建议再次复习材料。 由于仅实施了部分解决方案,因此很难解决该问题。

暂无
暂无

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

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