Skip to content

Instantly share code, notes, and snippets.

@kappa7194
Last active December 13, 2015 21:29
Show Gist options
  • Save kappa7194/4977868 to your computer and use it in GitHub Desktop.
Save kappa7194/4977868 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
"use strict";
angular.module("MyModule", []).controller("MyController", ["$scope", function ($scope) {
$scope.items = [{ name: "Apple", value: null }, { name: "Banana", value: null }, { name: "Orange", value: null }];
$scope.setItemValue = function (item) {
if (item.value === null || item.value === "") {
alert("You must specify a value.");
return;
}
if (!$scope.isValueValid(item)) {
alert("The specified value is not valid.");
return;
}
};
$scope.isValueValid = function (item) {
if (typeof item.value === "undefined") {
return false;
}
if (item.value === null || item.value === "") {
return true;
}
return /^[0-9]+(\.[0-9]{1,2})?$/.test(item.value);
};
}]);
$(document).ready(function () {
angular.bootstrap(document, ["MyModule"]);
});
</script>
</head>
<body>
<h1>Test</h1>
<div data-ng-controller="MyController">
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th></th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="item in items">
<td data-ng-bind="item.name"></td>
<td><input data-ng-model="item.value" type="number" /></td>
<td>
<input data-ng-click="$parent.setItemValue(item)" type="button" value="Set value" />
<span data-ng-hide="isValueValid(item)">The specified value is not valid.</span>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment