Angularjs <;选择>;1.3.0 rc0之后
我一直在使用1.3的beta版,现在转到1.3.1后,我注意到一个问题,通过检查所有早期版本,我发现它似乎是从1.3.0 rc1开始的 我有这样的代码:Angularjs <;选择>;1.3.0 rc0之后,angularjs,Angularjs,我一直在使用1.3的beta版,现在转到1.3.1后,我注意到一个问题,通过检查所有早期版本,我发现它似乎是从1.3.0 rc1开始的 我有这样的代码: <select ng-model="home.modal.topicId" ng-change="ctrl.modalTopicChanged()" ng-options="item.id as item.name for item in home.modal.option.topics.data"
<select ng-model="home.modal.topicId"
ng-change="ctrl.modalTopicChanged()"
ng-options="item.id as item.name for item in home.modal.option.topics.data"
ng-required="true">
<option style="display: none;" value="">Select Topic</option>
</select>
这里我注意到一个新函数:WriteToModelIfRequired
当我查看更改日志的差异时,在检查所有更改和行号时,我找不到任何关于引入此函数的内容
我想在这方面得到一些建议。首先,是否可以找到导致添加writeToModelIfNeeded的更改;其次,这是选择框的正确功能。我认为整个想法是,只有定义了模型值,ng更改才会触发
这里有一个新代码区域供参考,它似乎是用1.3.0RC.1添加的
**
* @ngdoc method
* @name ngModel.NgModelController#$commitViewValue
*
* @description
* Commit a pending update to the `$modelValue`.
*
* Updates may be pending by a debounced event or because the input is waiting for a some future
* event defined in `ng-model-options`. this method is rarely needed as `NgModelController`
* usually handles calling this in response to input events.
*/
this.$commitViewValue = function() {
var viewValue = ctrl.$viewValue;
$timeout.cancel(pendingDebounce);
// If the view value has not changed then we should just exit, except in the case where there is
// a native validator on the element. In this case the validation state may have changed even though
// the viewValue has stayed empty.
if (ctrl.$$lastCommittedViewValue === viewValue && (viewValue !== '' || !ctrl.$$hasNativeValidators)) {
return;
}
ctrl.$$lastCommittedViewValue = viewValue;
// change to dirty
if (ctrl.$pristine) {
ctrl.$dirty = true;
ctrl.$pristine = false;
$animate.removeClass($element, PRISTINE_CLASS);
$animate.addClass($element, DIRTY_CLASS);
parentForm.$setDirty();
}
this.$$parseAndValidate();
};
this.$$parseAndValidate = function() {
var parserValid = true,
viewValue = ctrl.$$lastCommittedViewValue,
modelValue = viewValue;
for(var i = 0; i < ctrl.$parsers.length; i++) {
modelValue = ctrl.$parsers[i](modelValue);
if (isUndefined(modelValue)) {
parserValid = false;
break;
}
}
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
// ctrl.$modelValue has not been touched yet...
ctrl.$modelValue = ngModelGet();
}
var prevModelValue = ctrl.$modelValue;
var allowInvalid = ctrl.$options && ctrl.$options.allowInvalid;
if (allowInvalid) {
ctrl.$modelValue = modelValue;
writeToModelIfNeeded();
}
ctrl.$$runValidators(parserValid, modelValue, viewValue, function() {
if (!allowInvalid) {
ctrl.$modelValue = ctrl.$valid ? modelValue : undefined;
writeToModelIfNeeded();
}
});
function writeToModelIfNeeded() {
if (ctrl.$modelValue !== prevModelValue) {
ctrl.$$writeModelToScope();
}
}
};
this.$$writeModelToScope = function() {
ngModelSet(ctrl.$modelValue);
forEach(ctrl.$viewChangeListeners, function(listener) {
try {
listener();
} catch(e) {
$exceptionHandler(e);
}
});
};
**
*@ngdoc方法
*@name ngModel.NgModelController#$commitViewValue
*
*@说明
*将挂起的更新提交到“$modelValue”。
*
*更新可能因取消公告事件而挂起,或者因为输入正在等待某个将来的消息
*在“ng模型选项”中定义的事件。作为“NgModelController”,很少需要此方法`
*通常处理调用此函数以响应输入事件。
*/
此.$commitViewValue=函数(){
var viewValue=ctrl.$viewValue;
$timeout.cancel(pendingDebounce);
//如果视图值没有改变,那么我们应该退出,除非有
//元素上的本机验证器。在这种情况下,即使
//viewValue保持为空。
如果(ctrl.$$lastCommittedViewValue===viewValue&&(viewValue!==''| |!ctrl.$$hasNativeValidators)){
返回;
}
ctrl.$$lastCommittedViewValue=viewValue;
//变脏
如果(ctrl.$pristine){
ctrl.$dirty=true;
ctrl.$pristine=false;
$animate.removeClass($element,PRISTINE_CLASS);
$animate.addClass($element,DIRTY_CLASS);
parentForm.$setDirty();
}
这是$$parseAndValidate();
};
此函数为$$parseAndValidate=function(){
var parserValid=true,
viewValue=ctrl.$$lastCommittedViewValue,
模型值=视图值;
对于(var i=0;i
通过这样做,我可以重现您的问题。没有看到您的控制器,但不确定是否相同:
this.modal = {
topicId:null,
option:{
topics:{
data:[{id:1,name:'item1'},{id:2,name:'item2'}]
}
}
};
这里发生的是angular说null是无效值,所以默认情况下将其设置为undefined。您可以通过将其设置为“未定义”或将其添加到html中来修复此问题:
ng-model-options="{allowInvalid:true}"
还测试了Josep plunker,将该值更改为null也导致ngChange触发您是否尝试创建一个带有1.2x和1.3x切换的JSFIDLE?我无法复制此问题。你介意分享一个复制这个问题的plunker的修改版本吗?谢谢我将研究这个plunker,看看是否能找到该实现与我的实现之间的差异。在引入变更的地方,您应该尝试在plunker中复制这种隔离。正在进行形式重新分解工作。请参阅[#8264][1]和[#6928][2],其中包含表格的重新分解。很可能是#6928导致了这种变化。[1]: [2]:
ng-model-options="{allowInvalid:true}"