如何在数组中存储新对象

时间:2017-06-09 11:06:01

标签: javascript angularjs

我有一个HTML页面: -

<div class="form-group">  
  <label>Template:</label>
    <input type="text" class="form-control" ng-model="a.Template">

  <label class="col-md-6">Name:</label>
    <input type="text" class="form-control" ng-model="a.Name">

    <label class="col-md-6">Class:</label>
      <input type="text" class="form-control" ng-model="a.Class">

    <label class="col-md-4">Code:</label>
        <input type="text" class="form-control" ng-model="a.Code">
    <label class="col-md-4">Item Name:</label>
        <input type="text" class="form-control" ng-model="a.ItemName">
</div>


             <table cellpadding="0" cellspacing="0" >
                        <thead>
                            <tr>
                                <th>Template</th>
                                <th>Name</th>
                                <th>Class</th>
                                <th>Code</th>
                                <th>Item Name</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-repeat="row in List track by $index">
                                <td>{{row.Template}}</td>
                                <td>{{row.Name}}</td>
                                <td>{{row.Class}}</td>
                                <td>{{row.Code}}</td>
                                <td>{{row.Item Name}}</td>
                            </tr>
                        </tbody>
                  </table> 



 <button type="submit" ng-click="add()">Add</button>

在我的指令页面中: -

(function () {
    'use strict';
    angular.module('myApp.components')
        .directive('info', info);

    info.$inject = ['$http' , '$timeout' ];

    function info($http, $timeout) {
        return {
            restrict: 'EA',
            scope: {

            },
            link: function (scope, el, attrs) {
                scope.a = {};
                scope.List = [];

             scope.add = function () {
               scope.List.push(scope.newBsb);

            };

            },
            templateUrl: ''
        };
    }

})();  

我要做的是在我的“添加”上。按钮所有信息用户输入应添加到List数组中。我将用户输入存储在对象&#39;中。 我应该怎么做才能在数组中添加每个对象。

1 个答案:

答案 0 :(得分:0)

您的按钮:

<button ng-click="add()">Add</button>

这应该足以满足你的功能添加:

 function add() {
   List.push([$scope.a]);
 }
相关问题