我有一个由以下指令创建的输入字段:
.directive('googlePlaces', function(){
return {
restrict:'E',
replace:true,
scope: {location:'='},
template: function (elem, attrs) {
return '<div><div class="form-group" ng-class="{ \'has-error\' : '+attrs.form+'.google_places_ac.$invalid }"><label>Address*</label><input id="google_places_ac" required name="google_places_ac" type="text" class="form-control" placeholder="Address" /><p class="help-block" ng-message="required" ng-show="'+attrs.form+'.google_places_ac.$invalid">Message</p></div></div>'
},
link: function($scope, elm, attrs){
var autocomplete = new google.maps.places.Autocomplete($("#google_places_ac")[0], {});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
$scope.location = place.name + ',' + place.geometry.location.lat() + ',' + place.geometry.location.lng() + "," + place.formatted_address;
$scope.$apply();
});
}
}
})我一直在尽力为这个领域添加所需的验证,但它不起作用。对于HTML表单中的其他输入字段,我也是这样做的,它工作得很好。
这是相关的HTML:
<form name="registrationForm" ng-submit="register()" novalidate>
...
<div class="col-xs-12">
<google-places location=location form="registrationForm"></google-places>
</div>
...我尝试过许多不同的东西,scope: {location:'=', form:'='},$compile,只需直接添加名称registrationForm,或简单地添加form。都没有用。
如果有人能帮我做这件事,我会非常感激的:)
发布于 2015-10-26 20:52:54
你可以在很多方面做到这一点。这是其中的几个。
1)将消息的验证和显示、访问表单等与googlePlaces指令隔离开来,并将其控制权交给消费者,因为它是消费者真正关心的问题。他们可以完全控制如何显示、显示什么以及在哪里显示。这将避免任何更多的责任,谁将负责提供选择的指示。让您的指令要求ng-model并指定所需的属性。
所以一个粗略的实现应该是这样的。
.directive('googlePlaces', function() {
return {
require:'ngModel',
restrict: 'E',
replace: true,
scope: {
location: '=ngModel'
},
template: function(elem, attrs) {
return '<div><div class="form-group"><label>Address*</label><input id="google_places_ac" required name="google_places_ac" type="text" ng-model="locSearch" class="form-control" placeholder="Address" /></div><button type="button" ng-click="clear()">clear</button></div>'
},
link: function($scope, elm, attrs, ctrl) {
var autocomplete = new google.maps.places.Autocomplete($("#google_places_ac")[0], {});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
$scope.location = place.name + ',' + place.geometry.location.lat() + ',' + place.geometry.location.lng() + "," + place.formatted_address;
$scope.$apply();
});
$scope.clear = function() {
$scope.location = null;
}
}
}
});和
<google-places name="location" ng-model=location required
ng-class="{ 'has-error' : registrationForm.location.$invalid }">
</google-places>
<span class="help-block"
ng-show="registrationForm.location.$invalid">Please specify location</span>
angular.module('app', []).directive('googlePlaces', function() {
return {
require:'ngModel',
restrict: 'E',
replace: true,
scope: {
location: '=ngModel'
},
template: function(elem, attrs) {
return '<div><div class="form-group"><label>Address*</label><input id="google_places_ac" required name="google_places_ac" type="text" ng-model="location" class="form-control" placeholder="Address" /></div><button type="button" ng-click="clear()">clear</button></div>'
},
link: function($scope, elm, attrs, ctrl) {
var autocomplete = new google.maps.places.Autocomplete($("#google_places_ac")[0], {});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
$scope.location = place.name + ',' + place.geometry.location.lat() + ',' + place.geometry.location.lng() + "," + place.formatted_address;
$scope.$apply();
});
$scope.clear = function() {
$scope.location = null;
}
}
}
});.has-error input {
border: 2px solid red;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div ng-app="app">
<form name="registrationForm" ng-submit="register()" novalidate>
<div class="col-xs-12">
<google-places name="location" ng-model=location required ng-class="{ 'has-error' : registrationForm.location.$invalid }"></google-places>
<span class="help-block" ng-show="registrationForm.location.$invalid">Please specify location</span>
</div>
</form>
</div>
2)您可以通过2种方式将表单对象绑定到指令,并从那里控制验证和显示消息。您需要在输入上放置一个ng-model,这样才能正确地启动验证。
.directive('googlePlaces', function() {
return {
restrict: 'E',
replace: true,
scope: {
location: '=',
form:'='
},
template: function(elem, attrs) {
return '<div><div class="form-group" ng-class="{ \'has-error\' :form.google_places_ac.$invalid }"><label>Address*</label><input ng-model="selectedLocation" id="google_places_ac" required name="google_places_ac" type="text" class="form-control" placeholder="Address" /><p class="help-block" ng-message="required" ng-show="form.google_places_ac.$invalid">Message</p></div></div>'
},
link: function($scope, elm, attrs) {
var autocomplete = new google.maps.places.Autocomplete($("#google_places_ac")[0], {});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
$scope.location = place.name + ',' + place.geometry.location.lat() + ',' + place.geometry.location.lng() + "," + place.formatted_address;
$scope.$apply();
});
}
}
});
angular.module('app', []).directive('googlePlaces', function() {
return {
restrict: 'E',
replace: true,
scope: {
location: '=',
form: '='
},
template: function(elem, attrs) {
return '<div><div class="form-group" ng-class="{ \'has-error\' :form.google_places_ac.$invalid }"><label>Address*</label><input ng-model="location" id="google_places_ac" required name="google_places_ac" type="text" class="form-control" placeholder="Address" /><p class="help-block" ng-message="required" ng-show="form.google_places_ac.$invalid">Message</p></div></div>'
},
link: function($scope, elm, attrs) {
var autocomplete = new google.maps.places.Autocomplete($("#google_places_ac")[0], {});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
$scope.location = place.name + ',' + place.geometry.location.lat() + ',' + place.geometry.location.lng() + "," + place.formatted_address;
$scope.$apply();
});
}
}
}).has-error input {
border: 2px solid red;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div ng-app="app">
<form name="registrationForm" ng-submit="register()" novalidate>
<div class="col-xs-12">
<google-places location=location form="registrationForm"></google-places>
</div>
</form>
</div>
https://stackoverflow.com/questions/33353765
复制相似问题