使用angularjs推送数组中的元素
这段代码运行良好,输出我想要的内容,但现在我想将每个字符串或条目保存在一个数组中,这样我输入的数据就不会丢失。在本例中,它只适用于一个项目,我想添加多个项目并存储它们 HTML代码使用angularjs推送数组中的元素,angularjs,Angularjs,这段代码运行良好,输出我想要的内容,但现在我想将每个字符串或条目保存在一个数组中,这样我输入的数据就不会丢失。在本例中,它只适用于一个项目,我想添加多个项目并存储它们 HTML代码 <script src="angular.min.js"></script> <script src="second.js"></script> <link rel="stylesheet" type="text/css" href="bootstrap.min.
<script src="angular.min.js"></script>
<script src="second.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
<html ng-app="first">
<body ng-controller="second">
<form >
Enter YOur Name <input type="text" ng-model="product.name" placeholder="Enter Your Name" required><br>
Enter Your email <input type="email" ng-model="product.email" placeholder="Enter Your Email" required><br>
enter your pass<input type="password" ng-model="product.pass" placeholder="*******" required><br>
Enter Your Color <input type="radio" ng-model="product.radio" value="red" required><br>
select any of 1
<select ng-model="product.select">
<option>punjab</option>
<option>kpk</option>
<option>balochistan</option>
<option>peshawar</option>
</select>
<br>
<input type="submit" >
<input type="reset" >
</form>
{{product.name}}
{{product.email}}
{{product.pass}}
{{product.radio}}
{{product.select}}
</body>
</html>
我希望这就是你要找的 对多个对象使用单个数组 提交表单时,您的值应保存在
$scope.entrylist
数组中
控制器功能
function MainController($scope) {
$scope.color = {
name: 'none'
};
$scope.entrylist = [];
$scope.submit = function() {
var temp = {}
temp["name"] = $scope.product.name;
temp["email"] = $scope.product.email;
temp["password"] = $scope.product.pass;
temp["color"] = $scope.color.name;
temp["place"] = $scope.product.select;
$scope.entrylist.push(temp);
};
};
您在数组中有数组,或者会有任何对象,如首先是数组,然后是对象,然后是我要存储信息的数组。为此,您需要在数组中添加循环,以便获取该数组中的值,并将数据推送到所需的位置
function MainController($scope) {
$scope.color = {
name: 'none'
};
$scope.entrylist = [];
$scope.submit = function() {
var temp = {}
temp["name"] = $scope.product.name;
temp["email"] = $scope.product.email;
temp["password"] = $scope.product.pass;
temp["color"] = $scope.color.name;
temp["place"] = $scope.product.select;
$scope.entrylist.push(temp);
};
};