Angular JS ngclick ngrepeat无法正常工作

时间:2015-06-28 19:32:26

标签: javascript html angularjs

我在ng-repeat中点击了一个按钮,但该功能是否曾被调用过? 有人可以告诉我为什么吗?

<div ng-repeat="data in exploreResults">
    <button type="button" ng-click="showMap(data, exploreResults)" class="showMapExploreButton">{{data.name}}</button>
    <p>Address: {{data.address}}<br>
    Rating: {{data.rating == 0 ? 'No rating available' : data.rating}} <br>
    Coordinates: {{data.coordinates.latitude}}, {{data.coordinates.longitude}}</p>
    <div ng-show=data.showMap id="map-canvas-explore" style="float:right; height: 20%; width: 50%; padding-right:20px;"></div>
</div>

java脚本如下:

<script>
function showMap(data, all) {
    all.forEach(function(entry) {
            entry.showMap = false;
    });
    data.showMap = true;
    console.log(JSON.stringify(data));
    //initialize map
    function initializeMap() {
        var latLng = new google.maps.LatLng(data.coordinates.latitude, data.coordinates.longitude);
        var mapCanvas = document.getElementById('map-canvas-explore');
        var mapOptions = {
            center: latLng, 
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.HYBRID
        }
        var map = new google.maps.Map(mapCanvas, mapOptions);
        new google.maps.Marker({
            position: latLng,
            map: map,
            title: 'Destination'
        });
    }
    initializeMap();
    google.maps.event.addDomListener(window, 'load', initializeMap);
}
</script>

感谢您的帮助;)

2 个答案:

答案 0 :(得分:0)

更新: - 答案说明: - 您未在范围内绑定showMap()功能。将其与$scope.showMap = function(data, all){...}绑定,如下所示

  <script>
    $scope.showMap = function(data, all) {
        all.forEach(function(entry) {
                entry.showMap = false;
        });
        data.showMap = true;
        console.log(JSON.stringify(data));
        //initialize map
        function initializeMap() {
            var latLng = new google.maps.LatLng(data.coordinates.latitude, data.coordinates.longitude);
            var mapCanvas = document.getElementById('map-canvas-explore');
            var mapOptions = {
                center: latLng, 
                zoom: 15,
                mapTypeId: google.maps.MapTypeId.HYBRID
            }
            var map = new google.maps.Map(mapCanvas, mapOptions);
            new google.maps.Marker({
                position: latLng,
                map: map,
                title: 'Destination'
            });
        }
        initializeMap();
        google.maps.event.addDomListener(window, 'load', initializeMap);
    }
    </script>

答案 1 :(得分:0)

ng-click指令中的函数调用需要引用作用域上的函数,就像exploreResults应该是作用域变量一样。那是

mysqli

在相关控制器内。