push elements in an array using angularjs

时间:2016-08-31 12:24:06

标签: angularjs

This code works fine and outputs what I want to get but now I want to save each string or entry in an array so my entered data will not be lost. In this case, it is just working for one item I want to add multiple items and want to store them.

HTML code

<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>

js code

var app = angular.module("first", []);
app.controller("second", function($scope) {
    $scope.product = [{
        name: [],
        email: [],
        pass: [],
        radio: [],
        select: []
    }];
});

1 个答案:

答案 0 :(得分:1)

我希望这就是你要找的东西

使用具有多个对象的单个数组

在提交表单时,您的值应保存在$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);

   };
};

<强> FULL EXAMPLE