AngularJS Tutorial - Push value to array in scope








The following code shows how to push value to an array in scope.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
function MyCtrl($scope) {<!--from   ww w.  ja  v a2 s  .c  o  m-->
    $scope.foo = "I'm foo!";
    $scope.lines = [];

    $scope.addLine = function () {
        $scope.lines.push($scope.lines.length);
    };
}
</script>
</head>
<body>
  <div ng-app ng-controller="MyCtrl">
    <button ng-click="addLine()">Add a line</button>
    <div ng-repeat="line in lines">
        I'm a new line. {{foo}}
    </div>
</div>
</body>
</html>

The code above is rendered as follows: