简体   繁体   English

Angularjs javascript 模态

[英]Angularjs javascript modals

I'm using Angularjs for my SPA project.我在我的 SPA 项目中使用 Angularjs。 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)每个页面/视图都保存在一个单独的物理文件中(纯 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.打开模态对话框的触发器可能发生在多个位置,其中一些位置会打开相同的对话框,并且可能来自点击链接( <a>Click here</a>喜欢)、按钮,或者只是拦截点击输入字段。

I should add that, when presenting the "to-be" modals as normal pages (ie adding them to the route provider list), I can see the contents properly.我应该补充一点,当将“to-be”模式呈现为普通页面(即,将它们添加到路由提供者列表中)时,我可以正确地看到内容。

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?我应该如何调用对话的呈现?

EDIT: added a last step.编辑:添加了最后一步。

Here's how I would go about it.这就是我将如何去做。 Let's assume you are using Angular-ui-bootstrap modals.假设您正在使用Angular-ui-bootstrap模态。

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.首先,您需要一个“模态工厂”来包含模态的定义,这意味着它们将具有视图的 url、控制器名称以及您可能想要传递到控制器的任何变量。 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.到目前为止,您有一个创建模态实例的工厂,并返回该模态实例的 PROMISE。

Secondly of course you need sampleView.html and SelectionModalController to be defined in proper places and be included in the project.其次,您当然需要在适当的位置定义sampleView.htmlSelectionModalController并将其包含在项目中。 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.请记住,如果要使用定义模态实例时定义的sample变量,则需要将其注入控制器。 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.第三,在你想要打开模态的控制器中,你注入你的AppSharedModalFactory并简单地调用工厂函数并将你想要的变量传递给它,这当然是可选的,然后使用返回的承诺来解决你想要的任何事情通过关闭模式关闭后解决 - 这导致已解决的承诺 - 或被驳回 - 导致拒绝承诺。 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.最后,您可以在 html 中执行<a href="" ng-click="openSelectionModal('Hello World!')">click to open modal!</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM