[英]angularjs add value Cannot set property 'id' of undefined
我正在嘗試將新條目添加到我有此查詢的表中
$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”
並沒有添加任何內容。
你的問題就在於此
$scope.client.id = clientId++;
因為$scope.client
永遠不會定義。
要解決這個問題
$scope.client = {};
之前
$scope.addClient = function() { ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.