Images in JSON AngularJS

I'm new to AngularJS, so sometimes when I do some mistake that is obvious, I still can't figure out what is going wrong with my code. So saying, here is my doubt:

HTML code:

<body ng-controller = "Ctrl">
<script id="Page6.html" type="text/ng-template">
<div class="list card" style="background-color: beige">

  <div class="item item-icon-left">
    <i class="icon ion-home"></i>
    <input type="text" placeholder = "Enter display name" ng-model="user.nam">
  </div>

<a ng-click = "saveedit(user)"<button class="button button-clear">SAVE DETAILS</button></a>
</div>
</script>
</body>

CONTROLLER.JS

.controller('Ctrl',function($scope,$rootScope,ContactService){
$rootScope.saveedit=function(user) {    

        ContactService.save({names: user.nam, image:"images.jpg"},ContactService.getid("Donkey"));

    }
});

THIS IS THE SERVICE:

.service('ContactService', function () {
var items = [
        { id: 1, names: 'Dolphin',  image: 'dolphin.jpg',}, { id: 2, names: 'Donkey',  image: 'donkey.jpg'}, { id: 3,  empid: 'FG2043', image: 'penguin.jpg'}];

var im = [{image: ''}];
var ctr=0;
var uid=3;

this.save = function (contact,id) {
       ctr=0;
       for (i=0;i<items.length;i++) {
       if(items[i].id == id)
       {
       im[0].image= items[i].image;
       ctr=100;
       break;
       }
       }


            uid = (uid+1);
            contact.id = uid;

            items.push(contact);
            if (ctr==100 ) {
            alert("in save putting the image");

            items[contact.id].image = im[0].image; //doubt

            alert("finished putting image");
        }      
    }

    //simply search items list for given id
    //and returns the object if found

    this.getid = function (name) {

        for (i=0;i<items.length;i++) {
            if (items[i].names == name) {
                return (i+1);
            }
        }           

    }

    //simply returns the items list
    this.list = function () {
        return items;
    }
});

The problem I am facing is this: Everything works, except one thing. In ContactService, push() function, the line I have commented as //doubt is not getting executed.

The alert before it "in save putting the image" runs, but the alert "finished putting image" doesn't. What is the mistake there??

The problem here is that you're using the id's, which start at 1, to navigate in an array whose indexes start at 0.

To access the most recently pushed element, you should rather do :

items[contact.id - 1].image = im[0].image;

But you actually don't need to access the array : items[contact.id - 1] will return the object that you just pushed, and which is already referenced by variable contact, so you could just do :

contact.image = im[0].image;