Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


angularjs conditional ng class with a function triggering ridiculous amounts using -'javascript,.net,angularjs,asp.net-mvc'


angularjs conditional ng class with a function triggering ridiculous amounts  using -'javascript,.net,angularjs,asp.net-mvc'


  I checked similar Q&As but they didn't fix my issue.


I want to dynamically set the class of an input, consider the following Plunkr.

As you can see I have one field, defined like this:

<input type="text" ng-model="Person.Name" ng-class="{'mandatory': IsFieldMandatory('Name')}" />


That function has the following code:

self.IsFieldMandatory = function(field){
  var isMandatory = true;

  var value = $scope.Person.Name;

  $scope.Cnt = $scope.Cnt + 1;

  return value.length == 0;
}


As you can see, it triggers 11(!) times on a page load (or any further action like blur).

PS: The function does not work because I get this error:

Error: $rootScope:infdig Infinite $digest Loop


It occurs because of the $scope.Cnt increment. If put in comment, it works (but then I don't know the number of triggers).

PPS:

How can I use this?:

var value = $parse('Person.' + field);


That would be better because then the function would work for any field. Which it needs to, in the real world scenario.

Thx!
    

asked Sep 7, 2015 by rajesh
0 votes
402 views



Related Hot Questions



Walkin Jobs Opening



Government Jobs Opening


...