0

I would like to create a confirmation-dialog-header directive that will open a Bootstrap UI Modal that will be used like this:

<button class="btn" confirmation-dialog-header="Non Working Dialog">
  Non Working Dialog
</button>

Here is what I'm trying to do (CoffeeScript): Live Demo

app.directive 'confirmationDialogHeader', ->
  restrict: 'A'
  link: (scope, element, attrs) ->
    $confirmationDialog = $ """
      <div modal="confirmationDialog">
        <div class="modal-header">
          <h4>#{attrs.confirmationDialogHeader}</h4>
        </div>
        <div class="modal-body">
          I want to be hidden by default
        </div>
        <div class="modal-footer">
          <button class="btn" ng-click="confirmationDialog = false">
            OK
          </button>
        </div>
      </div>
    """

    $(document.body).append($confirmationDialog) 

    $(element).click ->  
      scope.confirmationDialog = yes

Unfortunately, the modal is not hidden by default, and clicking the button doesn't seem to have any effect.

Any help would be appreciated.

2 Answers 2

2
  1. When dynamically inserting DOM elements containing AngularJS directives, you need to $compile them to kick in AngularJS.
  2. In event callback, scope.$apply need to be invoked to trigger AngularJS evaluation loops.

Thus

app.directive 'confirmationDialogHeader', ($compile) -> # 1

   $(document.body).append($confirmationDialog) 
   $compile($confirmationDialog)(scope) # 1

   $(element).click ->  
     scope.confirmationDialog = yes
     scope.$apply() # 2

Working One

Sign up to request clarification or add additional context in comments.

Comments

0

You should use the dialog service. To hide the modal in your directive, wrap the entire dialog in a <div> with the classes hide,modal,fade.

5 Comments

It looks like you are writing your own directive for bootstrap. I'm suggesting you use the one the angular team has already written.
My directive should have some confirmation logic. What you suggest doesn't provide that. I need a custom directive that uses the modal directive they provide.
Look at the JavaScript on the project page, you can see up buttons and capture results from the dialog. A dialog can have its own controller which could do verification and then pass the results back to the parent controller
Thanks! I'll have a look. But, still, I would be interested to know why the code above isn't working as expected.
See my update, I think you are missing a class on the modal header.

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.