Javascript 使用ng if重新定义ng模型

Javascript 使用ng if重新定义ng模型,javascript,angularjs,Javascript,Angularjs,因此,我有一个预先输入,用户可以在输入表单后从中选择选项。如果搜索数据存在,数据的描述会自动填入文本区域。但是,如果搜索数据不存在,说明框会自动填充搜索数据,这不是我想要的,因为它应该是空字符串,因为它不存在 <input type="text" required class="form-control form-control-modal" ng-model="newTreatment.async_selected_treatment" typeahead="treatment as t

因此,我有一个预先输入,用户可以在输入表单后从中选择选项。如果搜索数据存在,数据的描述会自动填入文本区域。但是,如果搜索数据不存在,说明框会自动填充搜索数据,这不是我想要的,因为它应该是空字符串,因为它不存在

<input type="text" required class="form-control form-control-modal" ng-model="newTreatment.async_selected_treatment" typeahead="treatment as treatment.service for treatment in searchTreatments($viewValue)" typeahead-editable="false" typeahead-on-select="searchTreatment($item)" focus="true" placeholder="CPT Code" >

<textarea class="form-control required form-control-modal" rows="3" ng-if="newTreatment.async_selected_treatment.id == 'null'" ng-model="newTreatment.async_selected_treatment.description" placeholder="Add a Description" ></textarea>
是null,因为它在后端不存在,所以我需要

newTreatment.async_selected_treatment.description 
一无所获

供参考的图片

我对解决这个问题的其他建议/想法持开放态度

工作

不起作用

您可以设置typeahead editable=false并添加ng blur指令。请参见此处:

HTML:


如果它实际上不存在,您应该检查===undefined,如果它实际上为null,您应该使用===null进行检查。此外,使用===而不是===几乎总是更好的,尤其是在检查null时,因为JS是邪恶的大师,当使用==时,很多东西都会导致错误。另外,我不太明白您在这里想要实现什么。如果ng If是隐藏的-您的描述听起来不像是您想要的-您可以共享angularjs代码和模板,以便我们可以找到哪里出了问题?很好的例子,但我的问题是它有两种不同的模型。第二个绑定到第一个使用plunkr进行复制太难了,我会给你要点让你看看
newTreatment.async_selected_treatment.description 
Model: {{selected | json}}
<input type="text" typeahead-editable="false"  ng-blur="clear()" ng-model="selected" typeahead="state for state in states | filter:$viewValue | limitTo:8" class="form-control">
if ($scope.selected === undefined) {

  $scope.selected = '';

}