0

I'm using Angularjs for my SPA project. So far, I'm managing to get what I want. Still, I'm failing to get modals to work and I need a simple explanation how to do it. Here are some details about my project structure:

  1. Each and every page/view is held in a separate physical file (pure HTML)
  2. For each page/view a controller is defined (again, each controller in its own physical file)
  3. I have defined a route provider that includes an entry for each page/view

The trigger for opening modal dialogs may take place from several locations, some of them opening the same dialogue, and it would come from clicking on a link (<a>Click here</a> like), buttons, or just intercepting click on input fields.

I should add that, when presenting the "to-be" modals as normal pages (i.e. adding them to the route provider list), I can see the contents properly.

My last attempt was following the example here with no success.

What I need is:

  1. How and where should I configure these modals (looked at many examples, and right now I'm completely lost)?
  2. How should I invoke the presentation of a dialogue?
halfer
  • 19,824
  • 17
  • 99
  • 186
FDavidov
  • 3,505
  • 6
  • 23
  • 59
  • Your first port of call should be here: https://angular-ui.github.io/bootstrap/, scroll down for the "Modal" directive. – Mourndark Jan 06 '16 at 13:02

1 Answers1

0

EDIT: added a last step.

Here's how I would go about it. Let's assume you are using Angular-ui-bootstrap modals.

Firstly you need a "Modal Factory" to contain the definitions of your modals, which means they'll have the url to the view, controller name, and any variable you might wanna pass onto your controller. For example:

"use strict";
angular.module("sampleModule").factory("AppSharedModalFactory", AppSharedModalFactory);

AppSharedModalFactory.$inject = ["$modal"];

function AppSharedModalFactory($modal) {
    var factory = {};
    factory.openSelectionModal = openSelectionModal;

    function openSelectionModal (sampleVar) {

        var modalInstance = $modal.open({
            templateUrl:     "sampleRoot/sampleFolder/sampleView.html",
            controller: "SelectionModalController",
            size: "lg", // or other sizes, read the docs
            sample: { // resolve helps with passing variables into a controller when you instantiate it
                module: function () {
                    return sampleVar;
                }
            }
        });

        return modalInstance.result;
    };
    return factory;
};

So far you have a factory that creates a modal instance, and returns the PROMISE of that modal instance.

Secondly of course you need sampleView.html and SelectionModalController to be defined in proper places and be included in the project. Remember that if you want to make use of the sample variable defined while defining the modal instance, you need to inject it into your controller. Example:

"use strict";
angular.module("sampleModule").controller("SelectionModalController", SelectionModalController);

SelectionModalController.$inject = ["sample"];

function SelectionModalController(myVariableNameForSample) {
};

Thirdly, in the controller that you want to open the modal on, you inject your AppSharedModalFactory and simply call the factory function and pass the variable you want to it, which is of course optional, and then use the returned promise to resolve anything you want to resolve after the modal has been closed by being closed -which results in a resolved promise- or dismissed -which results in a rejected promise. Example:

    $scope.openSelectionModal = function (sample) {
        appSharedModalFactory.openSelectionModal(sample).then(function (result) {
    console.log("yay I got a successful return.");
    }, function () {
    console.log("My modal was dismissed :(");
    });
};

And at last, you can do <a href="" ng-click="openSelectionModal('Hello World!')">click to open modal!</a> in your html.

Sina Gh
  • 598
  • 3
  • 8
  • Finally I got it working!!! You were absolutely right @SinaGh. I was missing the correct injection into the invoking controller. I really appreciate and thank you for your support, and obviously mark you answer as correct. – FDavidov Jan 12 '16 at 05:43
  • One more think if I could. I succeed to "SOMEWHAT" close the modal, but not completely. Using `$('#Generic_Modal').modal('toggle');` leaves the dark background, while the sequence `$('#Generic_Modal').modal('toggle'); $('body').removeClass('modal-open');$('.modal-backdrop').remove();` does not completely clean the screen (a small semi-transparent horizontal bar remains). Can you suggest how to do this correctly? (BTW, my tests are with Chrome). – FDavidov Jan 12 '16 at 08:51
  • What library are you using for your modals? – Sina Gh Jan 12 '16 at 09:02
  • Actually, none. I found [this one](https://github.com/jschr/bootstrap-modal), but not sure which file to include (i.e. add as – FDavidov Jan 12 '16 at 09:24
  • Without knowing what code you're using, how can I help you out with closing your modal? ^_^ – Sina Gh Jan 12 '16 at 09:26
  • You are (once again) right. I'll try adding the lib I mentioned above. Will post the results when done. – FDavidov Jan 12 '16 at 09:37
  • While checking things I suddenly realized that I'm using the code you (@SinaGh) suggested to open the modal, an hence if opening the modal works perfectly, it would look fair to assume that I'm using already the needed libs. As for general libs, JQuery, Angular, Bootstrap (and some of their sub-libs). The modal does close (using the sequence of 3 calls listed above) but it is not a "clean" closure (some shadows remain somehow). – FDavidov Jan 12 '16 at 13:03
  • `App.controller("ExampleModalController", ExampleModalController); ExampleModalController.$inject = ["$scope", "$modalInstance"]; function ExampleModalController($scope, $modalInstance) { $scope.ok = function () { $modalInstance.close("return value"); // triggers a success upon return. }; $scope.cancel = function () { $modalInstance.dismiss("other return value");// triggers a reject upon return. }; };` `ExampleModalController` is just an example name, you should use this code inside your modal's controller obviously. No need to play around with CSS. – Sina Gh Jan 12 '16 at 17:23
  • This is all according to the [docs](https://angular-ui.github.io/bootstrap/), which you should carefully study before you ask other people for help :) – Sina Gh Jan 12 '16 at 17:24
  • Reprimand accepted. This is my first WEB project and I'm learning all the related languages and tools (including basic HTML). So quite large amounts of material to study/read and no much time for that (you now how things are... things must be finished in time...). In any case, I much appreciate your help. Thanks. – FDavidov Jan 12 '16 at 17:37