0

I am trying to use Place Autocomplete using AngularJS, and I receive:

initAutocomplete is not a function

I searched for similar problems, nothing helped to solve the problem.

This is my code:

angular.module('myapp').controller('FirstCont', function ($scope, $http) {

console.log("OUT");

$scope.slctd_lng = 'SDF'
$scope.slct_att = 'SD'

window.initAutocomplete = function () {

    console.log("entered INIT");

    $scope.autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocomplete')),{ types: ['geocode'] });
    $scope.autocomplete.addListener('place_changed', $scocpe.fillInAddress);
}


$scope.fillInAddress = function () {
    var place = $scope.autocomplete.getPlace();

}


$scope.geolocate = function() {

    if (navigator.geolocation) {

        navigator.geolocation.getCurrentPosition(function (position) {

            var geolocation = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };

            var circle = new google.maps.Circle({
                center: geolocation,
                radius: position.coords.accuracy
            });

            $scope.autocomplete.setBounds(circle.getBounds());
        });
    }
}});

And in my html file, I included:

    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDsMQs29oSTadTMQI8Qy-VudC4N-kPvCpU&libraries=places&callback=initAutocomplete"
        async defer></script>

Note: I can see: (in the html file)

$scope.slctd_lng = 'SDF'
$scope.slct_att = 'SD'

but I did not receive 'OUT' in the console!!

Thanks

O. Dacca
  • 61
  • 1
  • 8

2 Answers2

0

Declare a function inside of your controller. $scope.initAutocomplete = function () ...; and then use it as $scope.initAutocomplete() or in HTML simply initAutocomplete()

hastrb
  • 410
  • 4
  • 12
  • I tried to declare it as $scope.. but this will not work, although this way I can see OUT in the log.. the init function should be run before loading the page, which means before running the controller, so it will not help if I use $scope – O. Dacca Aug 19 '17 at 12:41
  • @O.Dacca if you need to have it before, then you have got your answer. Angular controller is not a place for it. You may find this link useful [link](https://stackoverflow.com/questions/20663076/angularjs-app-run-documentation) – hastrb Aug 19 '17 at 12:43
0

It works now, I didn't change anything.. I rewrite the the init function and it works now, maybe it was a typing error.

thanks

O. Dacca
  • 61
  • 1
  • 8