Showing posts with label angular. Show all posts
Showing posts with label angular. Show all posts

Friday, October 24, 2014

reset valid for all form controls

I was trying to set $valid for all form controls, however I googled around and I do not see any solution to this on the internet. And I came out this myself as below:
    $scope.clearValidation = function () {
        angular.forEach($scope.managerUserForm, function (value, key) {
            var type = $scope.managerUserForm[key];
            if (type.$error) {
                angular.forEach(type.$error, function (value, key) {
                    type.$setValidity(key, 'true');                    
                });
            }
        });
    };
Or use this in modern browsers with ECMAScript 5 foreach:
Object.keys($scope.managerUserForm).forEach(function (key) {
            var type = $scope.managerUserForm[key];
            if (type.$error) {
                Object.keys(type.$error).forEach(function (key) {
                    type.$setValidity(key, 'true');                    
                });
            }
});
Or use underscore:
_.each($scope.managerUserForm, function(value, key){
    var type = value;
    if (type.$error) {
     _.each(type.$error, function(value, key){
        type.$setValidity(key, 'true'); 
     });
   }
});

Thursday, October 23, 2014

ng-readonly : Token '-100' is an unexpected token at column 10 of the expression

Token '-100' is an unexpected token at column 10 of the expression [user.Id!=='-100'] starting at ['-100'
 
 
ng-readonly="user.Id!=='-100'"
 
The issue disappears if the code changes to
 
    ng-readonly="user.Id!='-100'"
 Or ng-readonly="user.Id!==-100"

The reason?

ng-readonly or ng-show takes an "AngularJS statement." This type of statement only has an == operator, but this operator behaves like ===. It's a bit confusing.

Type coercion means that when the operands of an operator are different types, one of them will be converted to an "equivalent" value of the other operand's type. For instance, if you do:
boolean == integer
the boolean operand will be converted to an integer: false becomes 0true becomes 1. Then the two values are compared.
However, if you use the non-converting comparison operator ===, no such conversion occurs. When the operands are of different types, this operator returns false, and only compares the values when they're of the same type.