Curriculum
Course: AngularJS
Login

Curriculum

AngularJS

AngularJS Tutorial

0/65
Text lesson

Step 2. Adding Items:

In the HTML, include a text field and bind it to the application using the ng-model directive.

In the controller, create a function named addItem that takes the value from the addMe input field to add an item to the products array.

Then, add a button with an ng-click directive that triggers the addItem function when the button is clicked.

Example

We can now add items to our shopping list.

<script>
var app = angular.module(“myShoppingList”, []);
app.controller(“myCtrl”function($scope) {
  $scope.products = [“Milk”“Bread”“Cheese”];
  $scope.addItem = function () {
    $scope.products.push($scope.addMe);
  }

});
</script>

<div ng-app=”myShoppingList” ng-controller=”myCtrl”>
  <ul>
    <li ng-repeat=”x in products”>{{x}}</li>
  </ul>
  <input ng-model=”addMe”>
  <button ng-click=”addItem()”>Add</button>
</div>