Skip to content

Commit

Permalink
fix form getting dirty when ng-model has a value before selectize ini…
Browse files Browse the repository at this point in the history
…tializes
  • Loading branch information
machineboy2045 committed Oct 1, 2014
1 parent e6c5362 commit 630c293
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions dist/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

angular.module('selectize', []).value('selectizeConfig', {}).directive("selectize", ['selectizeConfig', '$timeout', function(selectizeConfig, $timeout) {

return {
return {
restrict: 'A',
template: '<select><option></option></select>',
replace: true,
Expand Down Expand Up @@ -78,13 +78,14 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
}


function updateAngularValue(value){
function updateAngularValue(val){
scope.$evalAsync(function(){
scope.ngModel = value;
scope.ngModel = val;
})
}

function updateSelectizeValue(curr, prev){
if(curr === prev) return;
//use timeout to wait in case options are being added
$timeout(function(){
selectize.setValue(curr);
Expand All @@ -96,15 +97,16 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
parseConfig();
element.selectize(config);
selectize = element[0].selectize;
selectize.setValue(scope.ngModel);

selectize.on('option_add', addAngularOption);
selectize.on('change', updateAngularValue)


scope.$watchCollection('ngModel', updateSelectizeValue);
scope.$watch('ngModel', updateSelectizeValue, true);
scope.$watchCollection('options', addSelectizeOptions);
scope.$watch('ngDisabled', toggle);


}
};
}]);

0 comments on commit 630c293

Please sign in to comment.