从指令控制器内的promise更新范围变量

时间:2015-04-10 19:38:27

标签: javascript angularjs angularjs-directive angularjs-scope angular-google-maps

我是棱角分明的新手。我试图从一个指令的控制器(来自它内部的一个承诺)填充控制器中存在的map.markers数组..并且很难搞清楚。任何帮助表示赞赏

var locationController = function ($scope, $http, AppModesService, limitToFilter, uiGmapGoogleMapApi) { //
        var latestMasterYear = AppModesService.getLatestMasterYear();
        uiGmapGoogleMapApi.then(function (maps) { $scope.googleVersion = maps.version; });
        $scope.map = {
                center: {
                    latitude: 40.0000, 
                    longitude: -98.0000,
                },
                zoom: 7,
                bounds: {},
                markers : []
        };

指令代码:

     app.directive('referencePoints', function () {
            return {
                restrict: 'E',
                scope: {
                    routeID: '@routeID',
                    road: '=',
                    openStatus: '=',
                    markers: '='
                },
                template: "{{routeID}}<div ng-repeat='point in refResults'>{{point.RoadName}}</div>",
                controller: function ($scope, $http, AppModesService) {
                    $scope.refResults;
                    $scope.isItOpen = true;
                    $scope.refResults = $http.get(AppModesService.getRisApiUrlPrefix() + 'GetReferencePoints?routesID=' + $scope.road + '&limited=true').then(
                            function (response) {
                                $scope.refResults = response.data;
// This is where the problem starts .. Should I inject $scope.markers here or not?
                                $scope.generateMarkers(response.data, $scope.markers)
                                //return response.data;
                            });
                    var generateMarkers = function (data, markers) {
                        for (i = 0; i < data.length; i++) {
                            var record = data[i];
                            var marker = {
                                latitude: record.Lat,
                                longitude: record.Long,
                                title: record.RoadName,
                                id: record.ID
                            };
                            markers.push(marker);
                        }
                        return markers;
                    };
                    $scope.generateMarkers = function(data, scope){
                        generateMarkers(data, scope);
                    };
                    //$scope.refPoints = $scope.referencePoints(routeID);
                }
            }
        });

...

 <reference-points ng-if="isItOpen" markers="markers" road=road.id route-ID="{{road.text}}"/>

...

另一条指令:

<ui-gmap-google-map center="map.center" zoom="map.zoom">
    <ui-gmap-markers models="map.markers" coords="'self'" icon="'icon'">

    </ui-gmap-markers>
</ui-gmap-google-map>

我无法从指令控制器将标记填充到地图中。请帮忙

1 个答案:

答案 0 :(得分:0)

我更改了标记=&#34;标记&#34;在referencePoints指令中标记=&#34; map.marker&#34;并修复了它。感谢@Matho的线索。