diff --git a/src/ngAutocomplete.js b/src/ngAutocomplete.js index 3b0b33c..0860963 100644 --- a/src/ngAutocomplete.js +++ b/src/ngAutocomplete.js @@ -39,46 +39,7 @@ angular.module( "ngAutocomplete", []) link: function(scope, element, attrs, controller) { - //options for autocomplete - var opts - var watchEnter = false - //convert options provided to opts - var initOpts = function() { - - opts = {} - if (scope.options) { - - if (scope.options.watchEnter !== true) { - watchEnter = false - } else { - watchEnter = true - } - - if (scope.options.types) { - opts.types = [] - opts.types.push(scope.options.types) - scope.gPlace.setTypes(opts.types) - } else { - scope.gPlace.setTypes([]) - } - - if (scope.options.bounds) { - opts.bounds = scope.options.bounds - scope.gPlace.setBounds(opts.bounds) - } else { - scope.gPlace.setBounds(null) - } - - if (scope.options.country) { - opts.componentRestrictions = { - country: scope.options.country - } - scope.gPlace.setComponentRestrictions(opts.componentRestrictions) - } else { - scope.gPlace.setComponentRestrictions(null) - } - } - } + var watchEnter = false; if (scope.gPlace == undefined) { scope.gPlace = new google.maps.places.Autocomplete(element[0], {}); @@ -97,13 +58,13 @@ angular.module( "ngAutocomplete", []) } else { if (watchEnter) { - getPlace(result) + getPlace(result); } } } - }) + }); - //function to get retrieve the autocompletes first result using the AutocompleteService + //function to get retrieve the autocompletes first result using the AutocompleteService var getPlace = function(result) { var autocompleteService = new google.maps.places.AutocompleteService(); if (result.name.length > 0){ @@ -136,8 +97,8 @@ angular.module( "ngAutocomplete", []) //on focusout the value reverts, need to set it again. var watchFocusOut = element.on('focusout', function(event) { element.val(detailsResult.formatted_address); - element.unbind('focusout') - }) + element.unbind('focusout'); + }); }); } @@ -146,7 +107,7 @@ angular.module( "ngAutocomplete", []) } }); } - } + }; controller.$render = function () { var location = controller.$viewValue; @@ -155,12 +116,28 @@ angular.module( "ngAutocomplete", []) //watch options provided to directive scope.watchOptions = function () { - return scope.options + return scope.options; }; scope.$watch(scope.watchOptions, function () { - initOpts() + if (scope.options) { + watchEnter = scope.options.watchEnter; + + if (scope.options.types) + scope.gPlace.setTypes([ scope.options.types ]); + else + scope.gPlace.setTypes([]); + + if (scope.options.bounds) + scope.gPlace.setBounds(scope.options.bounds); + else + scope.gPlace.setBounds(null); + + if (scope.options.country) + scope.gPlace.setComponentRestrictions({ country: scope.options.country }); + else + scope.gPlace.setComponentRestrictions(null); + } }, true); - } }; }); \ No newline at end of file