我想对表单的字段执行一些复杂的验证,其中字段验证绑定(bind)在一起或依赖于数据库信息,所以我不想测试 HTML 中的所有内容。目前,我有这个:
<select ng-options="i as i for i in parameters[offerType].SomeKey"
ng-model="itemSelected.aQuiteLongProperty[offerType].SomeKey.parameterValue"
ng-change="validateSomeKey(itemSelected.aQuiteLongProperty[offerType].SomeKey.parameterValue)">
</select>
---
$scope.validateSomeKey = function (val) {
// check if the data is consistent
};
如您所见,ng-model
有点冗长,所以我想避免 ng-change
指令中的重复。有没有办法访问select
/input
新值(选择的选项或输入的数据),不像How to Assign value with ng-change in angular js ,但以一种通用的方式?
我想要这样的东西:
<select ng-options="i as i for i in parameters[offerType].SomeKey"
ng-model="itemSelected.aQuiteLongProperty[offerType].SomeKey.parameterValue"
ng-change="validateSomeKey($this.value)">
</select>
我试图检查this
对象,我没有找到我想要的数据...
最佳答案
您不需要在 ngChange
中传递选定的值,它可以在 Controller /指令中使用 scope
访问:
$scope.validateSomeKey = function(){
/* do something with
$scope.itemSelected.aQuiteLongProperty[offerType].SomeKey.parameterValue */
}
或者你可以使用ngInit
<select
ng-init="selectedValue = model.aVeryLongParentProperty.aVeryLongChildProperty"
ng-model="selectedValue"
ng-options="i.name for i in items" ng-change="changed(selectedValue)">
<option value=""></option>
</select>
关于angularjs - 在 ngChange 指令中访问输入值的通用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24865557/