1

Here is my directive

.directive('closeMapMessage', function($log) {
  'use strict';
  return function(scope, element) {
    var clickingCallback = function() {
    angular.element('.map').fadeOut("slow");
    };
    element.bind('click', clickingCallback);
  };
})

How can I change a scope variable in the controller ?

<div class="msg-mobile" ng-show="showInstructionModal">
  <div class="close-map-msg ok-got-it-footer" close-map-message>Ok, got it. </div>
</div>

I basically want to set my showInstructionModalfalse when my close directive is called.

7
  • 1
    What about scope.showInstructionModal = false; scope.$apply(); at the end of your directive? Commented Apr 14, 2015 at 13:27
  • Why aren't you using angular modals? Or ngClick for that matter? Commented Apr 14, 2015 at 13:31
  • @Anzeo DOM manipulation should be done using directive, so he used directive which is correct Commented Apr 14, 2015 at 13:51
  • @pankajparkar There's no DOM manipulation here. It's a directive to fade out a modal and close it. That can be tackeled either by using Angular UI's modal service or alternatively could be fixed by adding a ng-click attribute and a ng-class that will toggle the fade out. Commented Apr 14, 2015 at 14:07
  • @Anzeo I looke ` angular.element('.map')` before applying fadeOut() ,thats why i love to use directive Commented Apr 14, 2015 at 14:08

2 Answers 2

0

From the current snippet of code, it's hard to tell why you're not using a modal solution tailored for Angular, i.e. AngularUI's modal.

However, in your current code, you're attaching a click event to the element outside of Angular's awareness. That's why clicking on the element will not have effect until the next $digest cycle has run. Also, in Agular you normally don't use directives the way you're trying to do. I would suggest updating the directive to also provide the HTML and then use the ng-clickattribute to attach the event handler via Angular.

Update your directive's code to:

.directive('closeMapMessage', function($log) {
  'use strict';
  return {
      restrict: "AE",
      link: function(scope, element) {
         scope.closeModal = function() {
             angular.element('.map').fadeOut("slow");
             scope.showInstructionModal = false; // probably need to put this in a $timeout for example to show the fading of the element
         };
      },
      template: '<div class="close-map-msg ok-got-it-footer" ng-click="closeModal()">Ok, got it.</div>'
  };
})

And then update your HTML accordingly:

<div class="msg-mobile" ng-show="showInstructionModal">
  <close-map-message></close-map-message>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You should run digest cycle manually after click event occurrence to update all scope bindings

.directive('closeMapMessage', function($log) {
  'use strict';
  return function(scope, element) {
    var clickingCallback = function() {
      angular.element('.map').fadeOut("slow");
      scope.$apply();
    };
    element.bind('click', clickingCallback);
  };
})

Comments

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.