如何使用angularjs中的push()添加数据

时间:2016-05-30 16:32:24

标签: angularjs

我想问一下,如何在用户点击按钮后使用push()添加数据。有人能帮助我吗?

{{1}}

1 个答案:

答案 0 :(得分:0)

form应该是一个数组(数组有push方法)。它是这样的:

HTML:

<div ng-controller="MyCtrl">
  <input type="text" data-ng-model="comName" />
  <input type="text" ng-model="comAddress" />
  <input type="text" ng-model="nameStaff" />
  <input type="text" ng-model="idStaff" />
  <button ng-click=addDetail()>add
  </button>
</div>

JS:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {

  $scope.form = [{
    companyName: "company1",
    companyAddress: "company address",
    staff: [{
      name: "men",
      id: "123"
    }]
  }]

  $scope.addDetail = function() {
    $scope.form.push({
      companyName: $scope.comName,
      companyAddress: $scope.comAddress,
      staff: [{
        name: $scope.nameStaff,
        id: $scope.idStaff
      }]
    });
  }

}

请参阅http://jsfiddle.net/royscn4j/