1

我有三個角的UI的自舉typehead的形式ng-model爲什麼沒有定義?

<form> 
<div class="form-group"> 
    <label for="fieldname" class="col-md-3 control-label">Name</label> 
    <div class="col-md-6"> 
     <input type="text" ng-model="newItem.customSelected" typeahead="name as name.name for name in members | filter:{name:$viewValue}" class="form-control" /> 
    </div> 
</div> 
<div class="form-group"> 
    <label for="fieldhname" class="col-md-3 control-label">House name</label> 
    <div class="col-md-6"> 
     <input type="text" ng-model="newItem.customSelected1" typeahead="house_name as house_name.house_name for house_name in family | filter:{house_name:$viewValue}" class="form-control" /> 

    </div> 
</div> 
<div class="form-group"> 
    <label for="" class="col-md-3 control-label"><?php echo $this->lang->line('label_family_id'); ?></label> 
    <div class="col-md-6"> 
     <input type="text" ng-model="newItem.customSelected2" typeahead="fm as fm.family_reg_no for fm in family | filter:{family_reg_no:$viewValue}" class="form-control" /> 
    </div> 
</div> 
<div class="col-md-3"></div> 
<input type="button" class="finish btn-success btn" ng-click="search(newItem)" value="Search"/> 
</form> 

搜索按鈕調用的功能,

$scope.search = function(item) { 
    item['id']=item.customSelected.id; 
    item['family_id']=item.customSelected1.id; 
    item['family_id']=item.customSelected2.id; 
    delete data.customSelected; 




    FamilyMemSearch.get(item, function (response) { // success 

      $scope.tableData = response.data; // store result to variable 

      }, function (error) { // ajax loading error 
       Data.errorMsg(); // display error notification 
      }); 

     }; 

,但它顯示了一個錯誤customSelected1,customSelected2是未定義的。

+3

你可以共享的jsfiddle或花掉來證明這​​個問題? –

+0

也許是因爲他們沒有「id」的屬性? – Goodnickoff

+0

我給了id.but再次顯示錯誤 –

回答

4

ngModel自動創建屬性值改變後第一次。如果您未初始化item.customSelected1item.customSelected2並在頁面加載後單擊搜索,則值將爲undefined

嘗試初始化您item.customSelected1item.customSelected2

app.controller("yourController",function($scope){ 
    $scope.newItem= {}; 
    $scope.newItem.customSelected1 = {}; 
    $scope.newItem.customSelected2 = {}; 
}); 
+0

再次顯示錯誤 –

+0

@Nisham Mahsin:我的錯誤,它應該是'newItem'而不是'item'。我更新了答案 –