0

How can one create a reusable model window in AngularJS?

I'd like it to be reusable in the sense that we trigger the modal window with a hyperlink and set the popup content based on the parameter passed. For example:

<div ng-controller="SampleCtrl">
    <a ng-click="toggleModal('FirstParam')">first</a>
    <a ng-click="toggleModal('SecondParam')">second</a>
    <a ng-click="toggleModal('ThirdParam')">Third</a>
</div>

When the first link is clicked it would display a modal containing content related to "FirstParam", etc.

I've tried custom directives and using $parent but I don't know how to pass the parameter from the toggleModal function call into the custom directive.

How can I achieve a re-usable modal window that is similar to this example?

In particular, I would like to know:

  • How can I pass variables from a controller function into a custom directive?
  • Are there any good tutorials on reusable custom directives and scopes?
  • Are there any other design patterns for reusable custom directives?
0

1 Answer 1

1

Checkout http://egghead.io for some basics on writing custom directives, also the angular docs on directive and compile have all the details https://docs.angularjs.org/guide/directive https://docs.angularjs.org/api/ng/service/$compile

Here's a plnkr I posted with an overridden template for the popover from ui-bootstrap. Depending on your exact use case you may be able to get away with just overriding a template or else making a directive that makes use of $modal or some other modal UI control http://plnkr.co/edit/eeiJ5re7mNdhHNDEeCQO?p=preview

// Code goes here

angular.module("myApp", ["ui.bootstrap", "ngSanitize"]).controller("TestCtrl", function($scope){


})

angular.module("template/popover/popover.html", []).run(["$templateCache", function ($templateCache) {
        $templateCache.put("template/popover/popover.html",
            "<div class='popover {{placement}}' ng-class='{ in: isOpen(), fade: animation() }'>" + 
            "<div class='arrow'></div><div class='popover-inner'>" + 
            "<h3 class='popover-title' ng-bind='title' ng-show='title'></h3>" + 
            "<div class='popover-content' ng-bind-html='content'></div>" +
            "<button class='btn btn-cancel' ng-click='manualHide()'>Cancel</button>" +
            "<button class='btn btn-apply' ng-click='manualHide()'>Apply</button></div></div>");
    }]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answering. I've gone through angularjs.org and custom directives however I was struck at "How to pass parameter from controller to directive 'link' function" If I know this I would dynamically load templates based on the hyperlink.
Basically you just need to read the details in the compile documentation about scope definition details

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.