为什么 ng-model 没有定义?
[英] Why ng-model is not defined?
本文介绍了为什么 ng-model 没有定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个表单中有三个 angular-ui-bootstrap typehead
<div class="form-group"><label for="fieldhname" class="col-md-3 control-label">房屋名称</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 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 class="col-md-3"></div><input type="button" class="finish btn-success btn" ng-click="search(newItem)" value="Search"/></表单>
搜索按钮调用函数,
$scope.search = function(item) {item['id']=item.customSelected.id;item['family_id']=item.customSelected1.id;item['family_id']=item.customSelected2.id;删除 data.customSelected;FamilyMemSearch.get(item, function (response) {//成功$scope.tableData = response.data;//将结果存入变量}, function (error) {//ajax 加载错误数据.errorMsg();//显示错误通知});};
但它显示错误 customSelected1,customSelected2 is undefined.
解决方案
ngModel
仅在值第一次更改后自动创建属性.如果您不初始化 item.customSelected1
、item.customSelected2
并在页面加载后立即单击搜索,则值将为 undefined
.
尝试初始化您的 item.customSelected1
和 item.customSelected2
app.controller("yourController",function($scope){$scope.newItem= {};$scope.newItem.customSelected1 = {};$scope.newItem.customSelected2 = {};});
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
});
};
but it shows an error customSelected1,customSelected2 is undefined.
解决方案
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
.
Try initializing your item.customSelected1
and item.customSelected2
app.controller("yourController",function($scope){
$scope.newItem= {};
$scope.newItem.customSelected1 = {};
$scope.newItem.customSelected2 = {};
});
这篇关于为什么 ng-model 没有定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文