小编qwd*_*412的帖子

Angular代码在第二次单击后工作

我正在服务器端使用Java编写一个互联网商店,并在客户端使用角度.在使用评论部分后,我遇到了一个问题.第二次单击"发送"按钮后,我的评论列表会刷新,并且之前写下了评论.所以,要查看我的评论,我需要先点击将其发送到服务器,然后再刷新注释数组(但第二次点击后它会发送新评论但不会渲染它).请帮忙,我是棱角分明的新手

var productApp = angular.module('productApp', []);
productApp.controller('productCtrl', ['$scope', '$location', '$http', function ($scope, $location, $http) {
  var url = $location.absUrl() + '/comments';
  $scope.comments = [];
  function getAllComments() {
    $http.get(url).then(function (response) {
        $scope.comments = response.data;
    });
  }

  var sendComment = function () {
    var comment = {'userName': 'danko', 'content': $scope.comment};
    $http.post(url, comment);

  };
  $scope.send = function () {
    sendComment();
    getAllComments();
  };
  getAllComments();
}]);
Run Code Online (Sandbox Code Playgroud)

模板:

  <ul class="media-list">
                    <li class="media">
                        <div ng-repeat="comment in comments" class="media">
                            <a class="pull-left">
                                <img class="media-object img-circle" src="/resources/img/user.png"
                                     style="width: 64px; height: …
Run Code Online (Sandbox Code Playgroud)

html javascript angularjs

0
推荐指数
1
解决办法
505
查看次数

标签 统计

angularjs ×1

html ×1

javascript ×1