user1937021
user1937021

Reputation: 10791

Add entries from the top in to-do list angular

I have made this to-do list in angular but would like the posts entered to entered fro m the top instead the bottom.

my code:

HTML

                  <a href="{{url.title}}" class="link">
                      <p class="title">{{url.name}}</p>
                      <p class="url">{{url.title}}</p>
                  </a>
              </div>
              <div class="col-md-4 delete m-b-2">
                    <!--a href="javascript:" ng-click="edit($index)" type="button" class="btn btn-primary btn-sm">Edit</a-->
                    <a href="javascript:" class="btn btn-danger btn-sm" ng-click="del($index)">Delete</a>

              </div>
        </div>
        </li>

JS

var urlFire = angular.module("UrlFire", ["firebase"]);
function MainController($scope, $firebase) {
  $scope.favUrls = $firebase(new Firebase('https://lllapp.firebaseio.com/'));
  $scope.urls = [];
  $scope.favUrls.$on('value', function() {
  $scope.urls = [];
  var mvs = $scope.favUrls.$getIndex();
    for (var i = 0; i < mvs.length; i++) {
      $scope.urls.push({
        name: $scope.favUrls[mvs[i]].name,
        title: $scope.favUrls[mvs[i]].title,
        key: mvs[i]
      });
    };
});

Upvotes: 0

Views: 70

Answers (2)

erichardson30
erichardson30

Reputation: 5054

Use $scope.urls.splice(index_to_insert,0, object); so in your case you could do

var obj = {
        name: $scope.favUrls[mvs[i]].name,
        title: $scope.favUrls[mvs[i]].title,
        key: mvs[i]
      };
$scope.urls.splice(0,0, obj);

Upvotes: 0

Zonedark
Zonedark

Reputation: 249

You can use unshift() instead of push() when you add elements to your array. It adds the element at the beginning of your array instead of at the end, and since your angular view is based on the model it will add it on top.

Upvotes: 2

Related Questions