標籤:blog module htm logs str cli 對象 click ng-model
angular js 實現表單提交時下面的table擷取到表單裡面的資料
<!DOCTYPE html>
<html >
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/bootstrap.min.css"/>
<script src="../angular-1.5.5/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="ctrl">
<form action="">
<p>姓名 <input type="text" ng-model="name"/></p>
<p>年齡 <input type="text" ng-model="age"/></p>
<p>性別 <input type="text" ng-model="sex"/></p>
</form>
<button type="submit" class="btn btn-info" ng-click="add()">提交</button>
<table class="table table-bordered" >
<thead>
<tr>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="elem in array">
<td>{{elem.name}}</td>
<td>{{elem.age}}</td>
<td>{{elem.sex}}</td>
</tr>
</tbody>
</table>
</body>
<script>
var app= angular.module("myApp",[]);
app.controller("ctrl",function($scope){
$scope.array=new Array(); //建立一個空數組
$scope.add=function(){
$scope.object=new Object(); //建立一個Null 物件用來放資料或者(var object=new Object())
$scope.object.name=$scope.name; /*($scope.object.name)為拓展對象裡面的name屬性*/
$scope.object.age=$scope.age;
$scope.object.sex=$scope.sex;
$scope.array.push($scope.object);/* 把對象裡面的資料添加到數組裡*/
console.log(this);
console.log($scope)
}
})
</script>
</html>
angular js 實現表單提交時下面的table擷取到表單裡面的資料