How to create angularjs custom directive for google location -


i'm trying create directive lookup google locations using <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=places&language=en-us"></script> this:

angular.module('myapp').directive('locationpicker', function () {      return {         restrict: 'ae',         replace: true,         require: 'ngmodel',         template: '<input id="{{id}}" type="text" class="{{class}}" placeholder="{{placeholder}}" />',         scope: {             id: '@',             class: '@',             placeholder: '@',         },         link: function ($scope, elm, attrs, controller) {              var autocomplete = new google.maps.places.autocomplete(elm[0], {types: ['geocode']});              var componentform = {                 locality: 'long_name',                 administrative_area_level_1: 'short_name',                 country: 'long_name'             };              google.maps.event.addlistener(autocomplete, 'place_changed', function () {                  var place = autocomplete.getplace();                 var lat = place.geometry.location.lat();                 var lng = place.geometry.location.lng();                  var name = "";                  (var = 0; < place.address_components.length; i++) {                     var addresstype = place.address_components[i].types[0];                     if (componentform[addresstype]) {                         if (name !== "") {                             name += ", ";                         }                          var val = place.address_components[i][componentform[addresstype]];                         name += val;                     }                 }                  elm[0].value = name;                 $scope.$apply(function () {                     controller.$setviewvalue({name: name, lat: lat, lng: lng});                 });             });         }     }; }); 

and input:

<input id="location" location-picker ng-model="location" class="form-control" placeholder="project location" ng-required="true" /> 

in controller:

$scope.location = {    name:null,    lat:null,    lng:null }; 

everything looks fine when component first rendered, value of input [object object] instead of place holder (project location).

what doing wrong?

problem

you binding ngmodel location object, renders [object object] when coerced string.

solution

since grabbing hold of ngmodelcontroller in directive, can use $formatters pipeline transform model value (location object name, lat, lng properties) view value , $render function specify how render value if changed outside of ngmodel lifecycle.

here working plunker: http://plnkr.co/edit/5hpfelbdfurzeccgfgyx?p=preview. crucial piece of code is

// triggered $setviewvalue controller.$formatters.push(function (value) {     return value ? value.name : value; });  // triggered clear() controller controller.$render = function () {     input.value = controller.$modelvalue.name; }; 

i have made following modifications code:

  1. used location-picker element directive. input not choice of element since has bindings ngmodel.
  2. removed replace: true. deprecated configuration option in directives, , can cause strange behaviors, since tries mix attributes of directive element , attributes of template element.
  3. removed elm[0].value = name;. handled lifecycle of ngmodel when call $setviewvalue.

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -