2

.controller('MapCtrl', [
    '$scope', '$http', '$location', '$window',
 
function ($scope, $http, $location, $window) {
    $http.get('****').success(function (data, dealers, response) {
        function initialize() {
            var serverData = data;
            $scope.locations = [];
            for (var i = 0; i < serverData.length; i++) {
                var modal = [
                data[i].Store_Name, data[i].S_Location.Latitude, data[i].S_Location.Longitude, i, 'images/arrow.svg', data[i].S_Address];
                $scope.locations.push(modal); 
            }
            console.log($scope.locations);
            //---------------------------------------------------------
            //console i am getting like this
            var locations = [
                ['nokia store', '12.971599', '77.594563', '1', 'images/arrow.svg.svg', '55a78953815356700bee698f'],
                ['samsung store', '12.9065534', '77.5774802', '2', 'images/arrow.svg.svg', '55a786d1815356700bee6982'], ];
            //----------------------------------------------------------
            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 13,
                center: new google.maps.LatLng(12.9667, 77.5667),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
            var infowindow = new google.maps.InfoWindow();
            var marker, i;
            for (i = 0; i < $scope.locations.length; i++) {
                //console.log($scope.locations[i][1]);
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng($scope.locations[i][1], $scope.locations[i][2]),
                    map: map,
                    icon: $scope.locations[i][4],
                    animation: google.maps.Animation.DROP,
                });
                google.maps.event.addListener(marker, 'click', (function (marker, i) {
                            return function() {
			//console.log($scope.locations[i][8]);

	  var compiled = $compile('<button ng-click="navigate('+$scope.locations[i][5]+')">Navigate</button>')($scope);

			var infowindow = new google.maps.InfoWindow({content: compiled[0]});
         	

          infowindow.open(map, marker);
		   $scope.$apply();
		   
        }
                })(marker, i));
            }
            $scope.map = map;
        }
        $scope.navigate(id) {
            console.log(id);
        }
    });

当我单击图标时,我在控制台中出现这样的错误

错误:[$parse:syntax] 语法错误:令牌 'b24782c7d354f30cda0e89' 是意外的,在表达式 [navigate(55b24782c7d354f30cda0e89)] 的第 12 列期望 [)] 从 [b24782c7d354f30cda0e89)] 开始。 http://errors.angularjs.org/1.3.13/ $parse/syntax?p0=b24782c7d354f30cda0e89&p1=is%20unexpected%2C%20expecting%20%5B)%5D&p2=12&p3=navigate(55b24782c7d354f30cda0e89)&p4=b24782c7d354f30cda0e89) at REGEX_STRING_REGEXP ( http://localhost:8100/bower_components/angular/angular.js:63:12 ) 在 Parser.throwError ( http://localhost:8100/bower_components/angular/angular.js:12011:11 ) 在 Parser.consume ( http://localhost:8100/bower_components/angular/angular.js:12053:12 ) 在 Parser.functionCall (http://localhost:8100/bower_components/angular/angular.js:12323:10 ) 在 Parser.primary ( http://localhost:8100/bower_components/angular/angular.js:11995:24 ) 在 Parser.unary ( http://localhost:8100/bower_components/angular/angular.js:12271:19 ) 在 Parser.multiplicative ( http://localhost:8100/bower_components/angular/angular.js:12254:21 ) 在 Parser.additive ( http://localhost:8100/bower_components/angular/angular.js:12245:21 ) 在 Parser.relational ( http://localhost:8100/bower_components/angular/angular.js:12236:21 ) 在 Parser.equality ( http://localhost:8100/bower_components/angular/angular.js:12227:21) (匿名函数)@ angular.js:11607$get @ angular.js:8557invokeLinkFn @ angular.js:8221nodeLinkFn @ angular.js:7729compositeLinkFn @ angular.js:7078publicLinkFn @ angular.js:6957(匿名函数)@控制器。 js:630S.trigger@main.js:20(匿名函数)@VM11388:37(匿名函数)@VM11381:10L.ff@VM11381:195L.Dk@VM11381:195S.trigger@main.js:20eb@main. js:22S.trigger@main.js:20L.Sk@VM11381:65(匿名函数)@main.js:21

4

1 回答 1

1

您收到$parse错误的解析错误是因为您的变量包含一个字符串,并且您在评估后locations[i][5]直接将该字符串放入函数中,因此在编译时会抛出错误,而在解析时会抛出错误。navigateng-click="navigate(55a78953815356700bee698f)"divdiv

您可以通过使用索引在该按钮内引用范围变量来解决此问题。

代码

$compile('<button ng-click="navigate(locations['+i+'][5])">Navigate</button>')($scope);
于 2015-07-25T06:21:46.120 回答