Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将对象推送到不在角度js中工作的数组_Javascript_Angularjs - Fatal编程技术网

Javascript 将对象推送到不在角度js中工作的数组

Javascript 将对象推送到不在角度js中工作的数组,javascript,angularjs,Javascript,Angularjs,我想将一个对象推送到数组中。但这并没有发生。我在提交点击时将对象注释(表单值)推送到dishDetailController的dish.comments。 ng controller=“dishcomentcontroller”嵌套在ng controller=“dishdailcontroller”中 我哪里做错了 main.html <div class="row row-content" ng-controller="dishDetailController as menuC

我想将一个对象推送到数组中。但这并没有发生。我在提交点击时将对象注释(表单值)推送到dishDetailController的dish.comments。 ng controller=“dishcomentcontroller”嵌套在ng controller=“dishdailcontroller”中 我哪里做错了

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>

如果我没有弄错的话,那么您从未创建过变量
$scope.dish
,而只创建了变量
var dish=…
,因此您不能分配任何内容;-)

在清除数组上查找javascript示例时遇到了这种情况。这个问题来自Coursera角JS课程的作业


如果正确使用$scope,则在初始化ng控制器时不需要使用“as MenuCtrl”。本课程将介绍这一点,我建议再次复习材料。解决这一问题将非常困难,因为只实施了部分解决方案

谢谢,我刚刚添加了$scope.dish=dish。“this.dish=dish”和“$scope.dish=dish”之间有什么区别?如果您可以解释或提供解释链接,将有很大帮助。当使用控制器作为语法时,这是正确的建议吗?
$scope
是控制器和视图之间通信的每个角度控制器的中心变量,在当前控制器的
$scope
下定义的所有变量也可以在html中访问$scope.dish={name:“Super Tasty”}将作为{{dish.name}或数据ng bind=“dish.name”访问。
this
运算符通常与构造函数一起使用,或者如果要引用当前定义的对象的属性,则此处根本不需要使用它。
 '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
            };
        }])


;