Skip to content

Instantly share code, notes, and snippets.

@jepzen
Last active October 19, 2016 13:27
Show Gist options
  • Save jepzen/a2099c492299c36be6f51d8fe93d6d1b to your computer and use it in GitHub Desktop.
Save jepzen/a2099c492299c36be6f51d8fe93d6d1b to your computer and use it in GitHub Desktop.
angular sample for adding people to a list
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
<h2>People</h2>
<ul>
<li ng-repeat="person in contacts">
<span>{{person.lastName}}, {{person.firstName}}</span>
<button ng-click="delete($index)">Delete</button>
</li>
</ul>
<input type="text" ng-model="firstName" size="30" placeholder="First name">
<input type="text" ng-model="lastName" size="30" placeholder="Last name">
<input class="btn-primary" type="button" ng-click="addNew()" value="add">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.contacts = [
{ firstName: 'John', lastName: 'Mogensen' },
{ firstName: 'John', lastName: 'Rambo' }
];
$scope.addNew = function () {
$scope.contacts.push({ firstName: $scope.firstName, lastName: $scope.lastName });
$scope.firstName = '';
$scope.lastName = '';
};
$scope.delete = function (idx) {
$scope.contacts.splice(idx, 1);
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment