为什么NG-模型没有定义?模型、定义、NG

2023-09-14 00:18:04 作者:红颜无痕伊人

我有三个角的UI引导typehead的形式

I have three angular-ui-bootstrap typehead in a form

<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>

搜索按钮调用函数,

the search button calls the function,

$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是不确定的。

but it shows an error customSelected1,customSelected2 is undefined.

推荐答案

ngModel 自动创建属性只。如果你不初始化 item.customSelected1 item.customSelected2 ,然后点击搜索页面加载时,值之后将未定义

ngModel automatically creates properties only after the value changes first time. If you don't initialize the item.customSelected1, item.customSelected2 and click search right after page loads, the values will be undefined.

尝试初始化你的 item.customSelected1 item.customSelected2

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