Angular JS ng-repeat/adding to array bug

Viewed 388

I'm very new to AngularJS development, and I was testing a very simple script when I came across a bug that I can't quite decipher.

My script simply adds a string to an array from an input box when the user types something and hits enter, and below the input box, all the items are then printed out using ng-repeat.

The script seems to work fine, except when I try to add a value that is already in the list. If I add a value that has already been added again, the script stops adding items, even if I go back and try a new value.

Here is my javascript:

    var myApp = angular.module('myApp',[]);
    myApp.controller('ctrl1',function($scope){
        $scope.list = [];

        $scope.addItem = function(keyEvent){
            if(keyEvent.which === 13)
            {

                if(angular.isDefined($scope.name) && $scope.name!='')
                {
                    $scope.list.push($scope.name);
                    $scope.name = '';
                }
            }
        }

    });

And here is the html portion:

<div ng-controller = "ctrl1">
<input type = "text" ng-model = "name" placeholder = "Enter item to add to list" ng-keypress = "addItem($event)"/>
<ul ng-repeat = "item in list">
<li>{{item}}</li>
</ul>
</div>

Does anyone know what is causing this bug? Thanks

1 Answers
Related