Save values of a form AngularJS

0

I have this form

<form>
                        <div class="form-group">
                            <label>Add a comment</label>
                            <input type="text" ng-model="newRole.newAuthor" placeholder="author">
                            <input type="date" ng-model="newRole.newDate">
                            <input type="file" ng-model="newRole.commentImage">
                            <textarea class="form-control metro" ng-model="newRole.newComment"></textarea>
                            <h2>{{txtcomment}}</h2> 
                        </div>
                    </form>
                    <!-- cierre de form para comentario -->

                    <button class="btn btn-primary" ng-click="trip.makeComment(newRole)">Comment</button>

And I want to save the values here inside:

this.tripObject.comments = [
        {
            "author": "Ronnie Oliver",
            "date": "05/06/16 01:19 PM",
            "imageURL": "/assets/images/placeholders/user.svg",
            "text": "Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Nullam id dolor id nibh ultricies vehicula ut id elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Nullam quis risus eget urna mollis ornare vel eu leo. Curabitur blandit tempus porttitor."
        },
        {
            "author": "Shaggy Rogers",
            "date": "05/06/16 12:48 PM",
            "imageURL": "/assets/images/placeholders/user.svg",
            "text": "Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Nullam id dolor id nibh ultricies vehicula ut id elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Nullam quis risus eget urna mollis ornare vel eu leo. Curabitur blandit tempus porttitor."
        }
    ];

But something I have wrong in the function or in the form I do not know

this.makeComment = function(newRole){
                // $scope.txtcomment = '';
                var newRole = {
                    "author":newRole.newAuthor,
                    "date":$scope.newDate,
                    "imageURL":$scope.commentImage,
                    "text" : $scope.newComment
                 }
                console.log($scope.newRole);



                console.log($scope.tripObject.comentario)


                this.tripObject.comments.push($scope.newRole);
                console.log(this.tripObject.comments);



            };
    
asked by Gabo Ruiz 26.06.2017 в 21:33
source

1 answer

0

How about, the object that has the data is the scope, or failing that you pass by parameter. If you notice you are overwriting the variable that passes through a parameter, then since it is not 100% necessary either, you can do the following:

this.makeComment = function(){
            var newRole = {
                "author":$scope.newRole.newAuthor,
                "date":$scope.newRole.newDate,
                "imageURL":$scope.newRole.commentImage,
                "text" : $scope.newRole.newComment
            }

            this.tripObject.comments.push(newRole);
            console.log(this.tripObject.comments);
        };

Another point is that I would call the fields exactly the same as the object in the array and that would prevent me from having to reprocess them in another object.

<input type="text" ng-model="newRole.author" placeholder="author">
<input type="date" ng-model="newRole.date">
<input type="file" ng-model="newRole.imageURL">
<textarea class="form-control metro" ng-model="newRole.text"></textarea>

and then, without reprocessing alone:

this.makeComment = function(){
     this.tripObject.comments.push($scope.newRole);
}
    
answered by 10.07.2017 в 01:12