简体   繁体   English

从指令控制器中的Promise更新范围变量

[英]update scope variable from a promise inside a directive controller

I am a newbie to angular. 我是新手。 I am trying to populate the map.markers array existing in a controller from a controller of a directive(from a promise inside it).. and having hard time figuring out. 我试图从指令的控制器(从内部的诺言)填充控制器中存在的map.markers数组。并且很难弄清楚。 any help is appreciated 任何帮助表示赞赏

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 : []
        };

Directive code: 指令代码:

     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}}"/>

.... ....

Another directive : 另一个指令:

<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>

I am unable to populate the markers to the map from the directive controller. 我无法从指令控制器将标记填充到地图。 Please help 请帮忙

I changed the marker="marker" in the referencePoints Directive to marker="map.marker" and that fixed it. 我将referencePoints指令中的marker =“ marker”更改为marker =“ map.marker”,并进行了修复。 Thanks @Matho for your clue. 感谢@Matho的线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM