Javascript 将google places条目限制为AngularJS中的建议列表
我已经将google places设置为自动填写表单输入。我现在需要某种验证。我决定最好的方法是不允许输入任何不在建议列表中的文本。我如何修改下面的工作指令来执行此操作?如果文本输入与有效建议不匹配,我希望清除文本输入并将模型设置为“” 角度指令:Javascript 将google places条目限制为AngularJS中的建议列表,javascript,angularjs,google-maps,google-maps-api-3,Javascript,Angularjs,Google Maps,Google Maps Api 3,我已经将google places设置为自动填写表单输入。我现在需要某种验证。我决定最好的方法是不允许输入任何不在建议列表中的文本。我如何修改下面的工作指令来执行此操作?如果文本输入与有效建议不匹配,我希望清除文本输入并将模型设置为“” 角度指令: .directive('googlelocation', function () { return { require: 'ngModel', link: function (scope, element, a
.directive('googlelocation', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, model) {
var options = {
types: [],
};
scope.gPlace = new google.maps.places.Autocomplete(element[0],
options);
google.maps.event.addListener(scope.gPlace, 'place_changed',
function () {
scope.$apply(function () {
model.$setViewValue(element.val());
});
});
}
};
});
<input class="pb-input" id="plocation" name="plocation" type="text" autocomplete="on" ng-model="formData.PLocation" googlelocation ng-required="true" >
供参考
HTML:
.directive('googlelocation', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, model) {
var options = {
types: [],
};
scope.gPlace = new google.maps.places.Autocomplete(element[0],
options);
google.maps.event.addListener(scope.gPlace, 'place_changed',
function () {
scope.$apply(function () {
model.$setViewValue(element.val());
});
});
}
};
});
<input class="pb-input" id="plocation" name="plocation" type="text" autocomplete="on" ng-model="formData.PLocation" googlelocation ng-required="true" >
脚本参考
<script src="//maps.googleapis.com/maps/api/js?sensor=false&libraries=places" type="text/javascript"></script>
尝试将
位置\u changed
侦听器更改为:
google.maps.event.addListener(scope.gPlace, 'place_changed',
function () {
scope.$apply(function () {
var place = scope.gPlace.getPlace();
if (!place.geometry) {
element.val('');
} else {
model.$setViewValue(element.val());
}
});
});
这越来越近了。从理论上讲,如果用户在列表中选择了不包含几何体的内容(即谷歌的建议),那么这个解决方案将有效,因此它将始终是正确的!如果Google发送了有效的结果,那么place.geometry将永远不会进行计算。我们需要的是,如果用户通过键入自己的文本来改变列表,而这不会验证。如果未选择建议,可能会触发getPlace事件来计算表达式。