Skip to content

Instantly share code, notes, and snippets.

@EmmanuelDemey
Created November 24, 2014 12:44
Show Gist options
  • Select an option

  • Save EmmanuelDemey/ea0126c8e1329a6ad0d6 to your computer and use it in GitHub Desktop.

Select an option

Save EmmanuelDemey/ea0126c8e1329a6ad0d6 to your computer and use it in GitHub Desktop.
Slide 38 - Use of $watch by Angular
<div data-ng-controller="ProductController" id="productForm">
Name *: <input type="text" id="inputName" data-ng-model="product.name"/>,
Price *: <input type="number" id="inputPrice" data-ng-model="product.price"/> €,
Quantity *: <input type="number" id="inputQty" data-ng-model="product.qty"/>
<br/> <i id="errorMsg">{{mandatoryMsg}}</i>
</div>
var ProductController = function($scope) {
$scope.product = { name: "", price: null, qty: null };
tmpMessage = "All fields are mandatory!";
$scope.$watch('product', function(newValue, oldValue) {
if(newValue.name === "" || newValue.price === null || newValue.qty === null){
$scope.mandatoryMsg = tmpMessage;
} else {
$scope.mandatoryMsg = "";
}
}, true);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment