ng click using angular and google maps

1

Good afternoon, cordial greeting, I wanted to comment that I have the following problem; I'm working with angular and google maps with the ng-map directive and I have my html, my app.js and my controller.js and the ng click does not work for me but when I do everything on a single page if it works for me, a recommendation? thank you very much, my code is as follows.

HTML

<!DOCTYPE html>
<html ng-app="premarca">
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script src="//maps.googleapis.com/maps/api/js?key=AIzaSyCZLRPtun19mn3xqSZi08dPp-1R4P2A2B4&sensor=false"></script>
    <script src="https://code.angularjs.org/1.3.15/angular.js"></script>
    <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>

</head>

<body ng-controller="mainController as vm">
    <div>
        <ng-map zoom="4" center="-28, 137.883">
            <map-data
                set-style="vm.styleFunc" 
                on-click="vm.onClick()"
                load-geo-json="data/ciudades.json">
            </map-data>
        </ng-map>
    </div>
    <button ng-click='buttonClick()'>Enviar</button>
    <script type="text/javascript" src='app.js'></script>
    <script type="text/javascript" src='controller/mainController.js'></script>

</body>

app.js

var app = angular.module('premarca', ['ngMap']);

controller.js

app.controller("mainController", function(NgMap,$scope) {
    var vm = this;

    alert('hola');

    var radioValue={};

    NgMap.getMap().then(function(map) {
        vm.map= map;
    });


    vm.onClick= function(event) {
        alert('hola');
    };

    $scope.buttonClick = function (){
        alert('hola');
    };
});//end aplication
    
asked by jcave 27.04.2017 в 21:15
source

1 answer

1

Be careful when using the controller functions within views when you use controllerAs . You have to homogenize the code and use controllerAs (I prefer this) or $scope .

Change

<button ng-click='buttonClick()'>Enviar</button>

for

<button ng-click='vm.buttonClick()'>Enviar</button>

and your method

$scope.buttonClick = function (){
    alert('hola');
};

for

vm.buttonClick = function (){
    alert('hola');
};
    
answered by 01.05.2017 в 15:46