0

Im basic developer , just wanted to reverse my array list after posting the form , Here is my code :-

app.js

    var app = angular.module('myApp');

app.controller('WorkingCtrl', ['$scope', '$http', function ($scope, $http) {

$scope.form = {};
$scope.post = [];
$scope.submitForm = function () {
    $scope.post.text = $scope.form.text;
        if ($scope.post.text != '') {
            $scope.post.push($scope.post.text);           
            $scope.post.text = '';

        }

        $scope.remItem = function ($index) {

         $scope.post.splice($index , -1);
        }


       }
 }]);

My html code is :-

      <!DOCTYPE html>
     <html ng-app="myApp">

     <head>
     <link rel="stylesheet" href="style.css">
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
     <script src="script.js"></script>
     <script>
      myApp = angular.module('myApp', []);

     </script>
     </head>

     <body>
     <div ng-controller="WorkingCtrl" id="divmsg">
     <form name="myForm" ng-controller="WorkingCtrl" novalidate>
     <textarea cols="75" rows="3" placeholder="Share Your Knowledge !" ng-model="form.text"></textarea>
            <div id="row">
            <ul id="msgHolder" data-bind="foreach: posts">
                <li ng-repeat="userpost in post">
                      <p>
                        <a>
                         <b>User_name</b>
                        </a>
                      <div>
                      <span class="wordwrap">
                        {{ userpost }}
                      </span>
                      </div>
                   </p>
                </li>
            </ul>
        </div>
         <input type="button" align="right" style="margin-top: 0px; margin-bottom: 0px; margin-left: 78px; padding-top: 0px;" id="submit" ng-click="submitForm()" class="shoutButton" value="Post">
        </form>
            </div>
           </body>
        </html>

Now this successfully gives me the post whatever the user submits from text box , but its going to the last of the array .

Which i want to achieve in first position . How to achieve that ? so that whatever user post will be seen first & not in the last . Thanks in advance . Here is a Plunker in case if neccessary :- http://plnkr.co/edit/pVnSgVE8pfaHK1PxgatM?p=preview

10

1 Answer 1

1

Use unshift instead of push.

$scope.post.unshift($scope.post.text); 

Working example: http://plnkr.co/edit/w1E3pcoWIBzHRRkib0jt?p=preview

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.