2014-11-04 8 views
7

Ho utilizzato una versione beta di 1.3 e ora dopo il passaggio a 1.3.1 noto un problema che verificando tutte le versioni precedenti vedo che sembra essere stato avviato in 1.3 .0 rc1.Problemi con ngChange per <select> dopo 1.3.0 rc0

Ho codice in questo modo:

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

Prima RC1 il ng-cambiamento è stato non essere licenziato quando il modulo è stato visualizzato per primo . Ora viene generato con un oggetto home.modal.topicId di non definito. Questo è un cambiamento irrisorio per me, ma non è menzionato nella sezione di cambio di rottura e mi chiedo se è un bug che deve ancora essere notato.

Ecco la traccia dello stack prodotto:

TypeError: Cannot read property 'dataMap' of undefined 
    at AdminProblemController.modalTopicChanged (http://127.0.0.1:17315/Content/app/admin/controllers/ProblemController.js:109:114) 
    at $parseFunctionCall (http://127.0.0.1:17315/Scripts/angular.js:11387:18) 
    at Scope.$get.Scope.$eval (http://127.0.0.1:17315/Scripts/angular.js:13276:28) 
    at http://127.0.0.1:17315/Scripts/angular.js:19888:13 
    at http://127.0.0.1:17315/Scripts/angular.js:19499:9 
    at forEach (http://127.0.0.1:17315/Scripts/angular.js:331:20) 
    at $$writeModelToScope (http://127.0.0.1:17315/Scripts/angular.js:19497:5) 
    at writeToModelIfNeeded (http://127.0.0.1:17315/Scripts/angular.js:19490:14) 
    at http://127.0.0.1:17315/Scripts/angular.js:19484:9 
    at validationDone (http://127.0.0.1:17315/Scripts/angular.js:19420:9) 

Quello che ho notato qui è una nuova funzione: writeToModelIfNeeded

Quando guardo le differenze registro delle modifiche non riesco a trovare alcuna menzione di questa funzione viene introdotta quando controllo tutte le modifiche e i numeri di riga.

Mi piacerebbe avere qualche consiglio su questo. In primo luogo è possibile trovare la modifica che ha causato l'aggiunta di writeToModelIfNeeded, quindi in secondo luogo si tratta della funzionalità corretta per la casella di selezione. Pensavo che l'idea fosse che il cambio ng si sarebbe attivato solo se il valore del modello fosse stato definito.

Per riferimento qui è l'area del nuovo codice che sembra essere stato aggiunto con 1.3.0 rc.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); 
     } 
    }); 
    }; 
+0

Hai provato la creazione di un jsfiddle con un interruttore per entrambi 1.2xe 1.3x? – John

+3

Non riesco a replicare questo problema, ** [vedi questo plunker] (http://plnkr.co/edit/KMIOZx7pV4OYmhk1XayE?p=preview) **. Ti dispiacerebbe condividere una versione modificata di questo plunker che riproduce il problema? Grazie! – Josep

+0

Guarderò in questo plunker e vedrò se riesco a trovare le differenze tra questa implementazione e la mia. Grazie –

risposta

5

sono stato in grado di riprodurre il problema in questo modo. Senza vedere il controller anche se non sono sicuro se stessa cosa:

this.modal = { 
     topicId:null, 
     option:{ 
     topics:{ 
      data:[{id:1,name:'item1'},{id:2,name:'item2'}] 
     } 
     } 
    }; 

Quello che sta accadendo qui è che angolare dice null è un valore non valido in modo da insiemi di default è undefined. È possibile risolvere questo problema impostando a 'undefined' o l'aggiunta di questo al vostro html:

ng-model-options="{allowInvalid:true}" 

anche testato plunker Josep e cambiando il valore null anche causato ngChange al fuoco

Problemi correlati