user2534381
user2534381

Reputation: 225

$http service executing multiple times in controller

I am using $http service to call server that return json data.howerver each time i request for the json data , the $http service is executing multiple times.dont know what went wrong.plz help. thanks in advance.below is my code.

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

       app.config(function ($routeProvider) {
       $routeProvider
    .when('/users',
    {
        templateUrl: "users.html",
        controller: "users"
    }

    ).when('/users/new',
    {
        templateUrl: 'new.html',
        controller : 'newuser'
    }).when('/users/:id/edit',
    {
        templateUrl: 'edit.html',
        controller: 'edit'
    })
    });


     app.controller('users',function($scope,$http){
     $scope.list_of_users = [];
     $http.get('http://testing.com:3000        /users.json').success(function(data,status,header,config){
    angular.copy(data,$scope.list_of_users)
     })
    });

   app.controller('newuser',function($scope,$http,$location){
    $scope.done = function(){
    var data = {user: {name: $scope.name}};
    $http.post("http://testing.com:3000/users.json",data).success(function(data,status,header,config){
        $location.path('/users');
    }).error(function(data,stauts,header,confi){
        });
        };
     });

   app.controller('edit',function($scope,$http,$routeParams,$location){
    $scope.name="";
    $http.get("http://testing.com/users/"+$routeParams.id +".json").success(function(data,status,header,config){
        console.log(data);
      $scope.name = data['user']['name'];
    });
   $scope.update = function(){
       var data = {user: {name:  $scope.name}};
       $http.put('http://localhost:3000/users/$routeParams.id',data).success(function(data,status,header,config){
             $location.path("/users");
       }).error(function(data,status,header,config){

           });
   }
   });

Upvotes: 0

Views: 1319

Answers (1)

JoakimB
JoakimB

Reputation: 1206

Most likly you defined your controller in both the routeProvider and in the actuall template ( ng-controller ). This makes it run multiple times. Remove one of them and try again.

Upvotes: 1

Related Questions