1

This's my service. I've got variable isPolygonDrawingEnded there.

angular
    .module('gillie.clients.app')
    .service('mapDrawingService', mapDrawingService);

mapDrawingService.$inject = ['$ngBootbox', '$translate', 'fitPolygonService', 'mapCommonService'];

function mapDrawingService($ngBootbox, $translate, fitPolygonService, mapCommonService) {
    var self = this;

    var map;
    var polygonFeatures;
    var stopDrawingAction;
    var isPolygonDrawingEnded = false;
    var isDrawingMode = true;

self.startNewPolygonDrawing = function (afterDrowed) {

        fitPolygonService.suggestPoint(isDrawingMode);
        var modify = createModifyInteractionToPolygonFeatures();

        var draw = createDrawInteraction();
        attachDrawEvents(draw, afterDrowed);
        map.addInteraction(draw);

        stopDrawingAction = function() {
            map.removeInteraction(draw);
            map.removeInteraction(modify);
        };
    };
        var attachDrawEvents = function (draw, afterDrowed) {
        draw.on('drawend', function (e) {
            e.preventDefault();
            afterDrowed();
            isPolygonDrawingEnded = true;

            if (fitPolygonService.isPolygonsExist()) {
                var geometry = e.feature.getGeometry();
                e.feature.setGeometry(fitPolygonService.fitPolygon(geometry));
            }
        });
}

This is my controller. Here I need to change $scope.showCountButton when mapDrawingService.isPolygonDrawingEnded == true. So how can I know that value in service is changed ?

angular
    .module('gillie.clients.app')
    .controller('regionController', regionController);

regionController.$inject = ['$q', '$filter', '$scope', '$ngBootbox', '$translate', 'regionService', 'mapService', 'mapDrawingService', 'modalService', 'regionCommonService', 'alertService', 'nominatimCommonService', 'populationService', 'provinceCommonService', 'provinceService', 'rx'];

function regionController($q, $filter, $scope, $ngBootbox, $translate, regionService, mapService, mapDrawingService, modalService, regionCommonService, alertService, nominatimCommonService, populationService, provinceCommonService, provinceService, rx) {

    $scope.showCountButton = false;

        $scope.startRegionSelection = function (region) {

        $scope.isSavingAvailable = true;
        $scope.showCountButton = false;
        if (currentEditingRegion && region && currentEditingRegion.Id === region.Id) {
            return;
        }

        if(region)
            mapService.clearRegion(region);

        if (currentEditingRegion && !region) {
            mapDrawingService.continuePolygonDrawing();
            return;
        }

        if (region) {
            mapDrawingService.stopPolygonDrawing();
            mapDrawingService.clearMap();

            currentEditingRegion = region;
            mapDrawingService.startExistPolygonDrawing(region.Name, region.CoordinatesJson);
            return;
        }

        mapDrawingService.startNewPolygonDrawing(function () {            
                    $scope.showCountButton = true           
        });
    };

When I use mapDrawingService.startNewPolygonDrawing function - the value of showCountButton changes but view doesn't. $scope.$apply produces this exception:

[$rootScope:inprog] $digest already in progress
1
  • Have a look at this Commented Nov 22, 2016 at 14:32

1 Answer 1

1

When you are passing the callback function it is no longer in the scope of the controller which is why the view is not updating. You can use promises instead since you are asynchronous draw event.

Service

mapDrawingService.$inject = ['$q', '$ngBootbox', '$translate', 'fitPolygonService', 'mapCommonService'];

function mapDrawingService($q, $ngBootbox, $translate, fitPolygonService, mapCommonService) {
  var self = this;

  var map;
  var polygonFeatures;
  var stopDrawingAction;
  var isPolygonDrawingEnded = false;
  var isDrawingMode = true;

  self.startNewPolygonDrawing = function() {
    fitPolygonService.suggestPoint(isDrawingMode);
    var modify = createModifyInteractionToPolygonFeatures();

    var draw = createDrawInteraction();
    var promiseObj = attachDrawEvents(draw);
    map.addInteraction(draw);

    stopDrawingAction = function() {
      map.removeInteraction(draw);
      map.removeInteraction(modify);
    };

    return promiseObj;
  };
  var attachDrawEvents = function(draw) {
    return $q(function(resolve, reject) {
      draw.on('drawend', function(e) {
        e.preventDefault();

        if (fitPolygonService.isPolygonsExist()) {
          var geometry = e.feature.getGeometry();
          e.feature.setGeometry(fitPolygonService.fitPolygon(geometry));
        }

        resolve();
      });
    });
  }

Controller

mapDrawingService.startNewPolygonDrawing().then(function() {
  $scope.showCountButton = true
});
Sign up to request clarification or add additional context in comments.

3 Comments

angular.js:13550 TypeError: Cannot read property 'then' of undefined at Scope.$scope.startRegionSelection (gillie.clients.region.controller.js:76) at fn (eval at compile (angular.js:14432), <anonymous>:4:297) at expensiveCheckFn (angular.js:15485) at callback (angular.js:25018) at Scope.$eval (angular.js:17229) at Scope.$apply (angular.js:17329) at HTMLButtonElement.<anonymous> (angular.js:25023) at HTMLButtonElement.dispatch (jquery.js:4737) at HTMLButtonElement.elemData.handle (jquery.js:4549) i`ve this exceptin in browser
Sorry, it should just be return promiseObj; not return promiseObj.promise;. I fixed it in the example above.
it`s realy good solution. thank you. Now i need to resolve another problem. Earlier i can't draw second polygon. Screenshots

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.