Skip to content

Instantly share code, notes, and snippets.

@poeticninja
Created January 20, 2014 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poeticninja/8526401 to your computer and use it in GitHub Desktop.
Save poeticninja/8526401 to your computer and use it in GitHub Desktop.
Simple Angular example showing how to use $scope, ng-bind, ng-click, and ng-class,
<!doctype html>
<html ng-app>
<head>
<meta charset="UTF-8">
<title>Angular Bots</title>
<style>
.dead {
background-color: red;
border: black 1px solid;
}
</style>
</head>
<body ng-controller="RobotsCtrl">
<ul ng-class="{dead: robotOne.dead}">
<li><strong>Name:</strong> <span ng-bind="robotOne.name"></span></li>
<li><strong>Health:</strong> <span ng-bind="robotOne.life"></span></li>
<li><strong>Strength:</strong> <span ng-bind="robotOne.strength"></span></li>
<li><button ng-click="attack(robotOne,robotTwo)">Attack</button></li>
</ul>
<ul ng-class="{dead: robotTwo.dead}">
<li><strong>Name:</strong> <span ng-bind="robotTwo.name"></span></li>
<li><strong>Health:</strong> <span ng-bind="robotTwo.life"></span></li>
<li><strong>Strength:</strong> <span ng-bind="robotTwo.strength"></span></li>
<li><button ng-click="attack(robotTwo,robotOne)">Attack</button></li>
</ul>
<script src="angular.js"></script>
<script src="scripts.js"></script>
</body>
</html>
function RobotsCtrl($scope){
var Robot = function(robotName, lifeNumber, strengthNumber){
this.name = robotName || 'prototype robot';
this.life = lifeNumber || 100;
this.strength = strengthNumber || 4;
this.dead = false;
}
$scope.robotOne = new Robot('Monkey Bot', 80, 9);
$scope.robotTwo = new Robot('Unicorn Bot', 70, 12);
$scope.attack = function(firstAttacker, secondAttacker){
secondAttacker.life = secondAttacker.life - firstAttacker.strength;
if (secondAttacker.life < 1) {
secondAttacker.dead = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment