Skip to content

Instantly share code, notes, and snippets.

@anonymoussc
Created October 25, 2015 20:58
Checking That Implicitly Defined Objects and Properties Exist
<!DOCTYPE html>
<html ng-app="exampleApp">
<head>
<title>Forms</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script>
angular.module("exampleApp", [])
.controller("defaultCtrl", function($scope) {
$scope.todos = [
{action: "Get groceries", complete: false},
{action: "Call plumber", complete: false},
{action: "Buy running shoes", complete: true},
{action: "Buy flowers", complete: false},
{action: "Call family", complete: false}];
$scope.addNewItem = function(newItem) {
if (angular.isDefined(newItem) && angular.isDefined(newItem.action)
&& angular.isDefined(newItem.location)) {
$scope.todos.push({
action : newItem.action + " (" + newItem.location + ")",
complete: false
});
}
};
});
</script>
</head>
<body>
<div id="todoPanel" class="panel" ng-controller="defaultCtrl">
<h3 class="panel-header">
To Do List
<span class="label label-info">
{{ (todos | filter: {complete: 'false'}).length}}
</span>
</h3>
<div class="row">
<div class="col-xs-6">
<div class="well">
<div class="form-group row">
<label for="actionText">Action:</label>
<input id="actionText" class="form-control" ng-model="newTodo.action">
</div>
<div class="form-group row">
<label for="actionLocation">Location:</label>
<select id="actionLocation" class="form-control" ng-model="newTodo.location">
<option>Home</option>
<option>Office</option>
<option>Mall</option>
</select>
</div>
<button class="btn btn-primary btn-block" ng-click="addNewItem(newTodo)">
Add
</button>
</div>
</div>
<div class="col-xs-6">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Action</th>
<th>Done</th>
</tr>
</thead>
<tr ng-repeat="item in todos">
<td>{{$index + 1}}</td>
<td>{{item.action}}</td>
<td>
<input type="checkbox" ng-model="item.complete"/>
</td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment