如何使用AngularJs绑定表中的数据?

时间:2018-10-10 05:19:28

标签: angularjs

我有一个像下面这样的简单表格

 <form novalidate name="f1" ng-submit="SaveData(User)">
      <b>Fname</b><input type="text" ng-model="User.fname" /><br />
     <b>Lnamew</b><input type="text" ng-model="User.lname" /><br />
                <input type="submit" />
                </form>

我的控制器是

app.controller('HomeCtrls', function ($scope) {
    $scope.msg = "Ok...."
    $scope.Getdata = [];
    $scope.SaveData = function (data) {
        $scope.Getdata.push(data)
    }
})

在这里我想将Getdata绑定到表

 <tr ng-repeat="formdata in Getdata">
  <td>{{formdata.User.fname}}</td>
  <td>{{formdata.User.lname}}</td>

1 个答案:

答案 0 :(得分:0)

这是您的答案,稍有改动

var app = angular.module('formApp',[]);
app.controller('formController',function($scope){

  $scope.Getdata = [];
  $scope.SaveData = function (data) {
     $scope.Getdata.push({
       fname:$scope.fname,
       lname:$scope.lname
     })
  
    console.log($scope.Getdata)
  }

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

<div ng-app="formApp" ng-controller="formController">


  <form novalidate name="f1" ">
      <b>Fname</b><input type="text" ng-model="fname" /><br />
      <b>Lnamew</b><input type="text" ng-model="lname" /><br />
       <button type="button" ng-click="SaveData()">Save</button>
  </form>

</div>