Angularjs Updates to ng-model
Testing for changes to ng-model
HTML
<script src="http://code.angularjs.org/1.1.0/angular.min.js"></script>
<div ng-app="update" ng-controller="homeCtrl">
<table>
<tr align="left">
<th>Street</th>
<th>City</th>
<th>State</th>
<th>Country</th>
</tr>
<tr ng-repeat="address in addresses">
<td><input type="text" ng-model="address.street" /></td>
<td><input type="text" ng-model="address.city" /></td>
<td><input type="text" ng-model="address.state" /></td>
<td><input type="text" ng-model="address.country" /></td>
</tr>
</table>
<button ng-click="update()">Update</button>
</div>
CSS
input {
width: 90%;
}
JavaScript
var app = angular.module('update', []);
app.controller('homeCtrl', function($scope) {
$scope.addresses = [
{
"street": "124",
"city": "Federal Way",
"state": "WA",
"country": "USA"
},
{
"street": "2345",
"city": "Provo",
"state": "UT",
"country": "USA"
},
{
"street": "3456",
"city": "Thousand Oaks",
"state": "CA",
"country": "USA"
}
];
$scope.originalList = angular.copy($scope.addresses);
$scope.update = function() {
var changes = false;
for (var i = 0; i < $scope.addresses.length; i++) {
if (($scope.addresses[i].street !== $scope.originalList[i].street) ||
($scope.addresses[i].city !== $scope.originalList[i].city) ||
($scope.addresses[i].state !== $scope.originalList[i].state) ||
($scope.addresses[i].country !== $scope.originalList[i].country))
{
changes = true;
// Address.update($scope.addresses[i]);
}
}
if (!changes) alert("No Changes Made.");
};
});