2016-07-15 2 views
3

как я упоминал в названии, я просто хотел перезагрузить (без обновления страницы) еще один контроллер с обновленными данными после POST здесь мой код.Angular Reload другой контроллер после POST

Нет необходимости сохранять данные в базе данных.

Script

var app = angular.module('userBase', []); 
app.controller('listUsers', function($scope,$http) { 
    $http.get("req.php") 
     .then(function (response) { 
      $scope.userloop = response.data.reqresponse; 
     }); 

}); 

app.controller('addUsers', function($scope,$http) { 

    $scope.buttonFire = function() { 
     $http.post("add.php",{'name': $scope.name, 'email': $scope.email}) 
      .success(function(data, status, headers, config){ 

      }); 
    } 

}); 

Посмотреть

<div ng-controller="listUsers"> 
    <ul ng-repeat="x in userloop"> 
     <li>{{ x.Name }} - {{ x.Email }} </li> 
    </ul> 
</div> 

<div ng-controller="addUsers"> 
    <p>Name: <input type="text" ng-model="name"></p> 
    <p>Email: <input type="text" ng-model="email"></p> 
    <span ng-bind="name"></span> and <span ng-bind="email"></span><br/> 
    <button ng-click="buttonFire()">Send or Save</button> 
</div> 

ответ

1

Вы можете решить проблему с помощью событий инфраструктуры:

app.controller('listUsers', function($scope,$http) { 
    $scope.on('newuser', function(event, data){ 
     load(true); 

     //or you can do this: (without row above, i.e. without performing 
     //request to the server) 
     //$scope.$apply(function(){ 
     //  $scope.userloop.push(data); 
     //}); 
    }); 

    var load = function(isEvent){ 
     $http.get("req.php") 
      .then(function (response) { 
       $scope.userloop = response.data.reqresponse; 
       if(isEvent) 
        $scope.$digest(); 
      }); 
    }; 
    load(); 
}); 

app.controller('addUsers', function($scope,$http) { 

    $scope.buttonFire = function() { 
     var newuser = {'name': $scope.name, 'email': $scope.email}; 
     $http.post("add.php", newuser) 
      .success(function(data, status, headers, config){ 
        $scope.$parent.$broadcast('newuser', newuser); 
      }); 
    } 

}); 
+0

Thank You So Much Это помогло M e :) – Jerushan

Смежные вопросы