Created
January 20, 2014 18:44
-
-
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,
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 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> |
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
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