Amy
Amy

Reputation: 163

Handle data in angularjs with loop

$scope.forms = [
        {id: 1, text: 'h_form'},
        {id: 2, text: 'S_form'},
        {id: 3, text: 'l_form'},
        {id: 4, text: 'g_form'},
        {id: 5, text: 'f_form'},
        {id: 7, text: 'b_form'},
        {id: 8, text: 's_form'}
      ];

I'm achieving some validation with these codes but thats weired called multiple times that's not correct

if($scope.form.userInfo.h_form != undefined){
        if($scope.form.userInfo.h_form.$invalid){
         if(setPlayerProfileId == ''){
          setPlayerProfileId = 'sportdetailform';
         }
        }
      }

      if($scope.form.userInfo.g_form != undefined){
        if($scope.form.userInfo.g_form.$invalid){
         if(setPlayerProfileId == ''){
          setPlayerProfileId = 'sportdetailform';
         }
        }
      }

      if($scope.form.userInfo.f_form != undefined){
        if($scope.form.userInfo.f_form.$invalid){
         if(setPlayerProfileId == ''){
          setPlayerProfileId = 'sportdetailform';
         }
        }
      }

I typically do not want to run this code multiple time so i used foreach function but doesn't work for me please suggest how to fixed out those

angular.forEach($scope.forms, function(value, key){
          var check = value.text;
            if($scope.form.userInfo.check != undefined){
              if($scope.form.userInfo.check.$invalid){
               if(setPlayerProfileId == ''){
                setPlayerProfileId = 'sportdetailform';
               }
              }
            }

      })

Upvotes: 1

Views: 37

Answers (1)

Vivz
Vivz

Reputation: 6620

Change your forEach in the following way:

angular.forEach($scope.forms, function(value, key){
        if($scope.form.userInfo[value.text] != undefined){
          if($scope.form.userInfo[value.text].$invalid){
           if(setPlayerProfileId == ''){
            setPlayerProfileId = 'sportdetailform';
           }
          }
        }

  });

Upvotes: 1

Related Questions