【发布时间】:2015-04-30 01:30:22
【问题描述】:
我已将 Google 地方设置为自动填写表单输入。我现在需要某种验证。我决定最好的方法是不允许输入不在建议列表中的任何文本。我如何修改下面的工作指令来做到这一点?如果文本输入与有效建议不匹配,我希望清除文本输入并将模型设置为 ''。
角度指令:
.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());
});
});
}
};
});
供参考
HTML:
<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>
【问题讨论】:
标签: javascript angularjs google-maps google-maps-api-3