简体   繁体   English

angularjs数据绑定问题

[英]angularjs data binding issue

I am trying to allow a user to submit a comment, where it can be displayed. 我正在尝试允许用户提交评论,并在其中显示该评论。 However, It doesn't seem to be working properly as I am able to get it to displayed as a preview but when I submit, it does not save as an array where the other comments are held. 但是,它似乎无法正常工作,因为我能够将其显示为预览,但是当我提交时,它并没有保存为包含其他注释的数组。 After debugging, I found out it was binding to my $scope.review, but it was being submitted to my array as a blank default format as shown on the webpage. 调试后,我发现它绑定到我的$ scope.review,但是它作为空白默认格式提交到我的数组,如网页上所示。 The source code can be found at http://plnkr.co/edit/q5fRIG6DzTegi3Ir1y8G?p=preview . 可以在http://plnkr.co/edit/q5fRIG6DzTegi3Ir1y8G?p=preview中找到源代码。 (Commented out the resetting of pristine state and scope, which also gives same error). (评论了原始状态和范围的重置,这也给出了相同的错误)。

Javascript 使用Javascript

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

        $scope.review = {name:"", rating:"Five", comment:"",date:""};
            $scope.submitComment = function () {

            $scope.review.date = new Date().toISOString();

            //  Push comment into the dish's comment array
            $scope.dish.comments.push("$scope.review");
             //reset  form to pristine
           // $scope.commentForm.$setPristine="";
             //reset  JavaScript object that holds your comment
           // $scope.review = {name:"", rating:"Five", comment:"",date:""};
        }

Html HTML

        <div class="media-list">
                <ul ng-show="!commentForm.comment.$error.required && !commentForm.comment.$pristine && !commentForm.name.$error.required && !commentForm.name.$pristine">
                    <blockquote>
                        <p>{{review.rating}} Stars</p>
                        <p>{{review.comment}}</p>
                        <footer>By {{review.name}} on {{review.date | date:'mediumDate'}} </footer>
                    </blockquote>
                </ul>
            </div>

您需要推送对象:

$scope.dish.comments.push($scope.review);

You are pushing a string, whereas you need to push an object: 您要推送一个字符串,而您需要推送一个对象:

$scope.dish.comments.push($scope.review);

Also, $scope.dish.comments belong to DishDetailController . 另外, $scope.dish.comments属于DishDetailController The submitComment method belongs to DishCommentController . submitComment方法属于DishCommentController The scope is controller defined, so they won't know about the existence of eachother. 范围是由控制器定义的,因此他们不会知道彼此的存在。

You need to put the methods in the same controller if you are expecting to share scope 如果希望共享作用域,则需要将方法放在同一控制器中

Agree with Lucas, you need to add the object rather than the string. 同意Lucas,您需要添加对象而不是字符串。 Also your new review field names don't match the existing fields in the existing reviews 另外,您的新评论字段名称与现有评论中的现有字段不匹配

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

            $scope.review = {
                                   rating:5,
                                   comment:"",
                                   author:"",
                                   date:""
                               };
                $scope.submitComment = function () {

                $scope.review.date = new Date().toISOString();

                //  Push comment into the dish's comment array
                $scope.dish.comments.push($scope.review);
                 //reset  form to pristine
                $scope.commentForm.$setPristine="";
                 //reset  JavaScript object that holds your comment
                $scope.review = {author:"", rating:5, comment:"",date:""};
            }

        }])

Notice I modified the "name" to "author" and the rating value from "Five" to 5. You will need to check the bindings in the "preview" html to match the changes though! 注意,我将“名称”修改为“作者”,将等级值从“五”修改为5。您将需要检查“预览” html中的绑定以匹配更改!

modified plunk here 这里改装的

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

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