Last active
October 19, 2016 13:27
-
-
Save jepzen/a2099c492299c36be6f51d8fe93d6d1b to your computer and use it in GitHub Desktop.
angular sample for adding people to a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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