Created
November 24, 2014 12:44
-
-
Save EmmanuelDemey/ea0126c8e1329a6ad0d6 to your computer and use it in GitHub Desktop.
Slide 38 - Use of $watch by Angular
This file contains hidden or 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
| <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> |
This file contains hidden or 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
| 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