我正在尝试向表中添加一个新条目我有此查询
$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>
它告诉我
Cannot set property 'id' of undefined
什么都没有添加。
最佳答案
你的问题是行
$scope.client.id = clientId++;
因为 $scope.client
从未被定义。
为了解决这个添加
$scope.client = {};
之前
$scope.addClient = function() { ... }
关于javascript - angularjs 添加值无法设置未定义的属性 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24699081/