简体   繁体   English

如何将数据推送到json服务器,然后将其显示在Angular / ionic的html页面中

[英]How to push data to json server and then display it in your html page in Angular/ionic

I am trying to make a dummy restaurant app using Angular/Ionic 1. I have created a modal to add comments and then i am pushing those comments to json server. 我正在尝试使用Angular / Ionic 1创建一个虚拟餐厅应用程序。我创建了一个添加评论的模式,然后将这些评论推送到json服务器。 I am trying to display the newly added comment in the html page along with other comments already being there. 我正在尝试在html页面中显示新添加的注释以及已经存在的其他注释。 The comment is getting pushed to json file but i am not able to display it on html page. 该评论被推送到json文件,但我无法在html页面上显示。 After the modal gets closed, the page gets stuck. 模态关闭后,页面被卡住。 How do i get to display the newly added comment? 我如何显示新添加的评论?

modal html code as below: 模态html代码如下:

    <ion-modal-view>
<ion-header-bar>
  <h1 class="title">Add Comments</h1>
   <div class="buttons">
        <button class="button button-stable button-clear"ng-click="closeComment()">Close</button>
    </div>
</ion-header-bar>

  <ion-content>
    <form id="addCommentForm" name="addCommentForm" ng-submit="submitComments()">
    <div class="list">
        <label class="item item-input item-select">
            <span class="input-label">Rating</span>
            <select ng-model="mycomment.rating">
                <option ng-repeat="n in [1,2,3,4,5]" value="{{n}}">
                    {{n}}</option>
            </select>
        </label>
        <label class="item item-input">
             <span class="input-label">Name</span>
            <input type="text" ng-model="mycomment.author">
        </label>
        <label class="item item-input">
            <span class="input-label">Comment</span>
            <textarea placeholder=""ng-model="mycomment.comment"></textarea>
        </label>
        <label class="item">
             <button class="button button-block button-positive" type="submit">Add Comment</button>
        </label>
    </div>
</form>
</ion-content>

Controller code as below: 控制器代码如下:

     $scope.mycomment = {rating:5, comments:"", author:"", date:""};

           $ionicModal.fromTemplateUrl('templates/dish-comment.html', {
              scope: $scope,
              animation: 'slide-in-up'
            }).then(function(modal) {
              $scope.addComment = modal;
            });
            $scope.openComment = function() {
              $scope.addComment.show();
              $scope.popover.hide();
            };

            $scope.closeComment = function() {
              $scope.addComment.hide();
            };


            $scope.submitComments = function () {

            $scope.mycomment.date = new Date().toISOString();
            console.log($scope.mycomment);

            $scope.dish.comments.push($scope.mycomment);
         menuFactory.getDishes().update({id:$scope.dish.id},$scope.dish);


            $scope.mycomment = {rating:5, comment:"", author:"", date:""};
            $scope.addComment.hide();
        }
            $scope.$watch('dish.comments', function() {
                $scope.test = $scope.dish.comments;
                console.log($scope.test);
                });

Below is the html page where i am trying to display the code 以下是我试图显示代码的html页面

    <div class="card">
      <div class="item item-body item-text-wrap">
           <img class="full-image" ng-src="{{baseURL+dish.image}}" alt="Uthappizza">
            <h2>{{dish.name}}
             <span style="font-size:75%">{{dish.price | currency}}</span>
            <span class="badge badge-assertive">{{dish.label}}</span></h2>
            <p>{{dish.description}}</p>
      </div>
  </div>
  <div class="row">
        <div class="col col-offset-10">
               <h4>Customer Comments &nbsp;&nbsp;&nbsp;
                   <small>Sort by: &nbsp;
                         <input type="text" ng-model="orderText">
                      </small></h4>
                <ul class="list">
                    <li ng-repeat="comment in dish.comments | orderBy:orderText">
                      <blockquote>
                         <p>{{comment.rating}} Stars</p>
                         <p>{{comment.comment}}</p>
                         <footer>{{comment.author}}, {{comment.date | date:'MMM. dd, yyyy'}}</footer>
                      </blockquote>
                    </li>
                </ul>
        </div>
  </div>

Below is my json file code, the last comment was newly added, but not showing in the html page. 以下是我的json文件代码,最近添加了最后一条注释,但未在html页中显示。

     {
"id": 3,
"name": "ElaiCheese Cake",
"image": "images/elaicheesecake.png",
"category": "dessert",
"label": "",
"price": "2.99",
"description": "A delectable, semi-sweet New York Style Cheese Cake, with Graham cracker crust and spiced with Indian cardamoms",
"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"
  },
  {
    "rating": "3",
    "comments": "Wonderful",
    "author": "Henna",
    "date": "2017-01-12T11:20:04.825Z"
  }
]

} }

Here is the recording of the app, i am trying to show that the screen freezes after the modal is closed with/without adding the comment 这是该应用程序的录音,我试图显示在关闭模态后添加/不添加评论的屏幕冻结

    https://i.stack.imgur.com/2lQhI.gif

So the page gets stuck with the oldest JSONS but doesn't display the newest data? 因此,页面陷入了最旧的JSONS之中,但不显示最新的数据吗? If thats the problem you can make a $watch function to always watch the $scope , so that way you can alway be monitoring the latest inserted data. 如果那是问题,则可以使$watch函数始终监视$scope ,以便始终可以监视最新插入的数据。

The code looks nice except for one thing, you are saving your modal instance in $scope.addComment variable and then naming your form the same name="addComment" . 该代码看起来很不错,除了一件事:将模态实例保存在$scope.addComment变量中,然后将表单命名为相同的name="addComment" In angular, the form is saved in a scope variable with it's name; 用角度表示时,表单会以其名称保存在范围变量中。 so basically it's trying to save the form AND the modal in $scope.addComment . 所以基本上,它试图将表单和模式保存在$scope.addComment

You should change the modal's or the form's name and try again, because probably when you submit the form, it gets saved in $scope.addComment and steps the modal instance, then you can't use it to close the modal. 您应该更改模式的名称或表单的名称,然后重试,因为可能当您提交表单时,它会保存在$scope.addComment并逐步执行模式实例,那么您将无法使用它来关闭模式。

EDIT: Apparently there is a known issue/unfixed bug with the ionic popover and the ionic modal. 编辑:显然有一个与离子弹窗和离子模态有关的已知问题/未修复的错误。 What you have to do is close the popover and don't open the modal until the next cycle. 您需要做的是关闭弹出窗口,直到下一个周期才打开模式。 You can achive that with $timeout or, even better, using a promise: 您可以使用$timeout甚至更好地使用promise来实现:

$scope.openComment = function () {
    $scope.popover.hide().then(openModal);
};

function openModal(){
    $scope.addComment.show();
}

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

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