1

I need to do a registration form page using angularjs and in this i need to show how many percentage have completed. I have more than 50 fields. How i can do this functionality in a simple way.

Here is the sample code. I don't know is this the good way of coding

HTML Code

 <script src="angular/angular.js"></script>
 <html ng-app="myapp" ng-controller='profileController'>
 <form>
   First name: <input type="text" name="firstname" ng-model="nameValue" ng-click="percentageCount()"/><br>
   Last name: <input type="text" name="lastname" ng-model="lnameValue" ng-click="percentageCount()"/>
   Age: <input type="text" name="age" ng-model="ageValue" ng-click="percentageCount()" />
   Gender: <input type="text" name="gender" ng-model="genderValue" ng-click="percentageCount()"/>
   City:  <select name="txt_country" class="drop-down-box" id="country" ng-click="percentageCount()" ng-model="countryValue">
                        <option value="" selected="selected">Select Country</option>
                        <option value="United States">United States</option>
                        <option value="United Kingdom">United Kingdom</option>
                        <option value="Afghanistan">Afghanistan</option>
                        <option value="Albania">Albania</option>                    
                      </select>

</form>
<p>{{count}}% completed</p>
</html>

Script

<script>

var myapp = angular.module('myapp', []);

myapp.controller('profileController', function ($scope,$http) 
{
    $scope.count = 0;
    $scope.percentageCount = function()
    {
        $scope.count =0;

        if($scope.nameValue != null)
          $scope.count = $scope.count+20;
        if($scope.lnameValue != null)
          $scope.count = $scope.count+20;  
        if($scope.ageValue != null)
          $scope.count = $scope.count+20;
        if($scope.genderValue != null)
          $scope.count = $scope.count+20;   
        if($scope.countryValue != null)
          $scope.count = $scope.count+20;       

    }

});

</script>

here I need to use lots of if condition.

In jquery we can do this using

$('input').on('change', function() 

How i can do this in angularjs as a good way of coding

Thank you in advance.

3 Answers 3

1

Instead of trying to recompute the count and storing the result in the scope every time something changes by binding an event listener, you can simply bind the count in the template to a function call:

<p>{{ percentageCount() }}% completed</p>

myapp.controller('profileController', function ($scope,$http) {
    $scope.percentageCount = function() {
        var count = 0;

        if ($scope.nameValue != null)
          count += 20;
        if ($scope.lnameValue != null)
          count += 20;
        if($scope.ageValue != null)
          count += 20;
        if($scope.genderValue != null)
          count += 20; 
        if($scope.countryValue != null)
          count += 20;     

        return count;
    }
});

At each digest cycle (every time some event is triggered and changes the scope), angular will call this function and refresh the value in the page if the result has changed. But since this function is simple and fast, that won't cause any problem.

If the rule for each property is always the same (increment of 100 divided by the number of properties), you can rewrite the above function like the following:

var props = ['nameValue', 'lnameValue', 'ageValue', 'genderValue', 'countryValue'];

$scope.percentageCount = function() {
        var count = 0;
        angular.forEach(props, function(prop) {
            if ($scope[prop]) {
                count += (100 / props.length);
            }
        });
        return count;
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

but i have more that 50 fields.
So what? That will only change the JavaScript percentageCount() function. That you'll have to implement whatever the solution you choose.
for all the 50 fields i have to check if it is null or not
See my edited answer. That's your business logic. It has nothing to do with your question. You would have to do something similar with jQuery as well, wouldn't you?
yes . i am new in angularjs. so I need a better solution
|
0

Angular provides a set of directives for all basic DOM manipulation. When starting to go AngularJS from JQuery world it's a good rule to always search for an appropriate directive and never use JQuery.

In your case ng-change will do the same trick similar to $('input').change()

2 Comments

but i have to give ng-change for all the input types. Is there anything that i can do similar like jquery in angular js
You should go model oriented, not DOM element oriented. Take a look at @SamuelNeff's answer. In the background it does the same but has a different all-in-one-place syntax. You list model properties or provide ng-change="updatePercentage()" to all inputs that are relevant
0

Instead of attaching event handlers to the inputs, watch the model for changes. Watch all of the model properties at once so if any changes your handler only gets called once for a group of changes.

var props = ['nameValue', 'lnameValue', 'ageValue', 'genderValue', 'countryValue'];

$scope.$watchGroup(props, valuesChanged);

function valuesChanged() {
    var count = 0;

    props.forEach(function(prop) {
        if ($scope[prop] != null) {
            count += 20;
        }
    });

    $scope.count = count;
}

4 Comments

so in html no need to give ng-click
@Athi what makes you think that? No, you don't. Every time you change something in an input, the ng-model directive will update the corresponding field in the scope. That will automatically cause the valuesChanged watcher to be called and thus the count to be recomputed.
'valuesChanged is not defined' its throws error is there anything i have to do any changes in this code
@Athi, valuesChanged is the function declared above. Did you copy the code exactly as above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.