AngularJS编辑数据/表格

时间:2015-08-01 13:10:20

标签: javascript json angularjs sql-update

我希望在AngularJS上创建一个表单来编辑通过REST API访问的数据库行。我没有访问数据的问题,我真正遇到的问题是配置表单进行编辑。我如何能够显示数据,如果用户单击提交按钮,则能够更新数据。下面是我的Angular代码。

   countryApp.controller('EditLocation', function($scope, $http, $routeParams, $location) { 
      //get the item information
      var id = $routeParams.locid; 
      $scope.activePath = null;    

      $http.get('http://localhost/slimtest2/location/'+id).success(function(data) { 
        $scope.location = data;  

      }); 

      $scope.editrel = function() { 
          var editData = { 
              location_title : $scope.l.location_title, 
              location_latitude : $scope.l.location_latitude, 
              location_longitude : $scope.l.location_longitude     
          } 

          //convert data to JSON string
          var loce = JSON.stringify(editData); 


          $http.put('http://localhost/slimtest2/location/edit', loce); 

      }

      $scope.reset = function() { 
        $scope.location = data; 
      } 


    }); 

HTML - 编辑表单

<div ng-controller="EditLocation"> 

   <form name="editform" ng-submit="editrel()"> 
      <div ng-repeat="l in location.location">  
     <label for="location_title">Location Title</label>
     <input type="text" ng-model="l.location_title"> 
     <br/> 
      <label for="location_latitude">Location Latitude</label>
     <input type="text" ng-model="l.location_latitude"> 
     <br/> 
        <label for="location_longitude">Location Longitude</label>
     <input type="text" ng-model="l.location_longitude"> 
     <br/>  


      <input type="submit" value="Submit"> 
      <input type="button" ng-click="reset()" value="Reset"></input>
     </div> 
    </form>
</div>

1 个答案:

答案 0 :(得分:5)

如果您的put响应中未包含新的位置数据,则可以通过以下方式获取数据:

$http.put('http://localhost/slimtest2/location/edit', loce).success(function () {
  $http.get('http://localhost/slimtest2/location/'+id).success(function(data) { 
    $scope.location = data;  
  });
});