0

I want to add data at the time of onclick event. Need to load image at the time of onclick event, after a small time interval add data. But my image is continuously loading. Any body give any suggestion.

My code is:

<!DOCTYPE html>
<head>
<title>Learning AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" type="text/javascript"></script>
<script language="javascript">
    function ContactController($scope) {
        $scope.contacts = [];    
        $scope.items = [ ];
        $scope.add = function() {
            setTimeout(function() {
                $scope.$apply(function() {
                    $scope.items[0].lateLoader = '  xxxx ';  
                });
            }, 1000);
            $scope.count=$scope.count+1;
            $scope.contacts.push($scope.newcontact);
            $scope.newcontact = "";
        }
    }
</script>
</head>
<body >
    <div ng-app="" ng-controller="ContactController">
        <p>{{items.lateLoader}} 
            <i ng-hide="items.lateLoader"><img src="Filling broken ring.gif"></i>
        </p>
        {{ contacts.length }}
        Content:<input type="text" ng-model="newcontact" />
        <button ng-click="add()">Add</button>
        <ul style="list-style-type: none;">
            <li ng-repeat="contact in contacts"> <input name="" type="checkbox" value="">{{ contact }}</li>
        </ul>
    </div>
</body>
</html>
1
  • 3
    You are using a very old version of Angular, and not instantiating correctly - I suggest you read some tutorials first and get the basic structure right. In addition, Angular provides a $timeout that plays better with the rest of Angular. Commented Dec 6, 2014 at 10:15

1 Answer 1

2

In your example I found a lot of mistakes. The HTML tag is not defined at the top, wrong use of angularJs and Angular module is not created properly etc.

I fixed all the mistakes. I hope it can help you.

Plunkr link: http://plnkr.co/edit/no8WOHdEc9wc3dHzzITv?p=preview

 <!DOCTYPE html>
 <html ng-app="app">  
    <head>
      <title>Learning AngularJS</title>
       <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
       <script >
       angular.module("app",[]).controller('ContactController',['$scope', 
        function($scope) {
         $scope.contacts = [];    
         $scope.items = [];
         $scope.add = function() {

         setTimeout(function() {
         $scope.$apply(function() {
         $scope.items.lateLoader = 'xxxx ';  
         });
         }, 1000);
       //$scope.count=$scope.count+1;
        $scope.contacts.push($scope.newcontact);
        $scope.newcontact = "";
       }
     }
   ]);
  </script>
  </head>
<body >
 <div ng-controller="ContactController">
 <p>{{items.lateLoader}} 
   <i ng-hide="items.lateLoader">
      <img src="https://encrypted-tbn1.gstatic.com
     /images?q=tbn:ANd9GcQTaHe0F0J39SXbiRF43pz2wtyfD6kypCMrLxhWPkq9EACNgwO0iaMbJFM"> 
   </i>
 </p>
{{contacts.length}}
 Content:<input type="text" ng-model="newcontact" />
 <button ng-click="add()">Add</button>
  <ul style="list-style-type: none;">
  <li ng-repeat="contact in contacts"> 
  <input name="" type="checkbox" value="">{{ contact }}  
    </li>
  </ul>
    </div>
    </body>
 </html>

And for more detail of angularJs please visit these links:
(https://angularjs.org/)
(http://www.w3schools.com/angular/default.asp)

Sign up to request clarification or add additional context in comments.

Comments

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.