Skip to content

Instantly share code, notes, and snippets.

@changemewtf
Created October 14, 2014 22:41
Show Gist options
  • Save changemewtf/822cfedc368c401f151a to your computer and use it in GitHub Desktop.
Save changemewtf/822cfedc368c401f151a to your computer and use it in GitHub Desktop.
Favorite ice cream flavors
angular.module('icecream', []).
controller('IceCreamController', function() {
this.flavors = [
'vanilla',
'chocolate',
'neopolitan',
'green tea'
];
this.faves = [
'vanilla'
];
this.addFlavor = function() {
this.flavors.push(this.newFlavor);
this.newFlavor = null;
};
this.isFave = function(flavor) {
return this.faves.indexOf(flavor) != -1;
};
this.toggleFave = function(flavor) {
if (this.isFave(flavor)) {
var index = this.faves.indexOf(flavor);
this.faves.splice(index, 1);
} else {
this.faves.push(flavor);
}
};
})
;
<!doctype html>
<html lang="en" ng-app="icecream">
<head>
<title>Ice Cream</title>
<style type="text/css">
#flavors li {
cursor: pointer;
}
.fave {
color: green;
}
</style>
</head>
<body ng-controller="IceCreamController as ctrl">
<ul id="flavors">
<li ng-repeat="flav in ctrl.flavors"
ng-class="{ fave: ctrl.isFave(flav) }"
ng-click="ctrl.toggleFave(flav)"
>{{ flav }}</li>
</ul>
New Flavor: <input ng-model="ctrl.newFlavor" />
<button ng-click="ctrl.addFlavor()">Add</button>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.5/angular.js"></script>
<script src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment