angularjs增加价值无法设置属性“ID”的未定义属性、价值、未定义、angularjs

2023-09-13 05:05:56 作者:听闻爱情十妄九悲

我想添加新条目到表我有这个疑问。

I'm trying to add a new entry to a table I have this query

$scope.addClient = function() {

  var clientId = $scope.items.length;
  //alert(empid);
  $scope.client.id = clientId++;
  $scope.items.push($scope.client);

}

这是我的表单

<div>
  <div class="modal-header">
    <h2>Ajouter client</h2>
  </div>    
  <form role="form"  ng-submit="addClient()">
    <div class="modal-body">
      <label for="name">Nom : </label>
      <input type="text" id="name" class="form-control input" ng-model="client.name" value="  {{client.name}}" />

      <label for="age">Age : </label>
      <input type="text" id="age" class="form-control input" ng-model="client.age" value="{{client.age}}" />

      <label for="gender">Sexe : </label>
      <input type="text" id="gender" class="form-control input" ng-model="client.gender"  value="{{client.gender}}" />

      <label for="email">Email : </label>
      <input type="text" id="email" class="form-control input" ng-model="client.email" value="{{client.email}}" />

      <label for="company">Société : </label>
      <input type="text" id="company" class="form-control input" ng-model="client.company" value="{{client.company}}" />
    </div>
    <div class="modal-footer">
      <button  type="submit" class="btn btn-primary" >Confirmer</button>
      <button   class="btn btn-warning" ng-click="cancel()" >Annuler</button>
    </div>
  </form>
</div>

它告诉我

无法设置的未定义的属性ID

Cannot set property 'id' of undefined

和添加什么。

推荐答案

您的问题是该行

$scope.client.id = clientId++;

由于 $ scope.client 是从来没有定义的。

because $scope.client is never defined.

要解决这个添加

$scope.client = {};

$scope.addClient = function() { ... }
 
精彩推荐