Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have an onClick angularjs function has a for loop that does not work in first click

I have an onClick angularjs function do some binding and has a for loop. my problem is that the for loop does not work in first click. but in second its ok. how can i fix it? in my app i have an array of students firstname= fname, lastname=lname, and tuitions. i want when i click on student name, the program show students fullname, and list of his or her tuitions.

HTML:

    <li ng-repeat="x in stdNames" class="text-center">
        <a ng-cloak ng-click="showStuInfo($index)" href="#stu-info">{{ x.fname + ' ' + x.lname}}</a>                    
    </li>

    //result box (in another page - loading by router )
    <section class="stuInfoBox clear">
        <div class="nameRow clear">
            <p class="text-right pull-right">Name</p><p class="text-right pull-right stu-fullname" ng-bind="studentName"></p>
        </div>
        <div class="tuitionsRow clear">
            <ul class="stuTuitionList">
            </ul>
        </div>
    </section>

JavaScript (AngularJS Controller):

     //Students array
     $scope.stdNames = [
            { stuId: "s01", fname: 'Mina', lname: 'Ghorbani', tuitions:['Javascript Programming','AngularJs','JQuery','C# Programming']};

   //onclick function
    $scope.showStuInfo = function (index) {
            $(".stuTuitionList li").remove();
            $scope.studentName = $scope.stdNames[index].fname + " " + $scope.stdNames[index].lname;
            var tuitionsLength = $scope.stdNames[index].tuitions.length;
            for (var i = 0; i < tuitionsLength; i++) {
                $(".stuTuitionList").append("<li>"+$scope.stdNames[index].tuitions[i]+"</li>");
            }

        }
like image 360
Mina Ghorbani Avatar asked Dec 02 '25 10:12

Mina Ghorbani


1 Answers

I've created a sample app with your example. It is working fine here, for the first time the click is getting initialised. Check the below.

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

app.controller('samplecontroller', function($scope) {

  //Students array
  $scope.stdNames = [{
    stuId: "s01",
    fname: 'Mina',
    lname: 'Ghorbani',
    tuitions: ['Javascript Programming', 'AngularJs', 'JQuery', 'C# Programming']
  }];

  //onclick function
  $scope.showStuInfo = function(index) {
    $(".stuTuitionList li").remove();
    $scope.studentName = $scope.stdNames[index].fname + " " + $scope.stdNames[index].lname;
    var tuitionsLength = $scope.stdNames[index].tuitions.length;
    for (var i = 0; i < tuitionsLength; i++) {
      $(".stuTuitionList").append("<li>" + $scope.stdNames[index].tuitions[i] + "</li>");
    }

  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="sample">
  <div ng-controller="samplecontroller">
    <li ng-repeat="x in stdNames" class="text-center">
      <a ng-cloak ng-click="showStuInfo($index)" href="#stu-info">{{ x.fname + ' ' + x.lname}}</a> 
    </li>

    //result box (in another page - loading by router )
    <section class="stuInfoBox clear">
      <div class="nameRow clear">
        <p class="text-right pull-right">Name</p>
        <p class="text-right pull-right stu-fullname" ng-bind="studentName"></p>
      </div>
      <div class="tuitionsRow clear">
        <ul class="stuTuitionList">
        </ul>
      </div>
    </section>
  </div>
</body>
like image 197
Nikhilesh Shivarathri Avatar answered Dec 04 '25 23:12

Nikhilesh Shivarathri