简体   繁体   English

角度/模态/ ngDialog-如何在具有嵌套/多个状态/视图的模态中具有多个控制器?

[英]Angular / modal /ngDialog - How to have multiple controller in a modal with nested / multiples states / views?

I used ngDialog and have a modal with nested views. 我使用了ngDialog,并具有带有嵌套视图的模式。 Insight my modal I am switching between signup (state) and login (login) so I need to include controller for signup and one for login. 洞察我的模态我正在注册(状态)和登录(登录)之间切换,因此我需要包括用于注册的控制器和一个用于登录的控制器。 I have been searching for a solution for hours everywhere but couldn't find anything. 我一直在各地寻找解决方案数小时,但找不到任何东西。 Does anyone how I can do that? 有人能做到吗? In case somebody knows a solution NOT for ngDialog but for $modal, please let me know too. 如果有人知道不是ngDialog而是$ modal的解决方案,请也告诉我。 Thanks!! 谢谢!!

    $scope.clickToOpen = function () {
       ngDialog.open({ 
          template: 'js/modal/modal.html',
          controller: 'SignupCtrl',
       });
    };

In the code above- everything works find for signup because I included its controller, but not for Login as I don't know how to include logins controller. 在上面的代码中,一切都可以找到注册,因为我包括了它的控制器,但找不到登录,因为我不知道如何包括登录控制器。 Also, if its important, I will include more code-just thought it doesn't matter for my question. 另外,如果它很重要,我将包括更多代码,只是认为这对我的问题无关紧要。

What I also tried is this: 我还尝试过的是:

app.config(function ($stateProvider) {

   $stateProvider.state('activity.modal', {
     url: 'modal',
     templateUrl: 'js/modal/modal.html',
     views: {
      'loginView@': {
        templateUrl: 'js/login/login.html',
        controller:'LoginCtrl'
      },

      'signupView@': {
        templateUrl: 'js/signup/signup.html',
        controller:'SignupCtrl'
      }
     }
    });

});

but that doesn't work neither for login nor for signup. 但这对登录和注册都无效。

There are multiple solutions to this problem. 有多种解决方案。 Some of them are better than others, but at the end of the day, it depends on what kind of UI/UX you are looking for. 其中有些比其他的要好,但是归根结底,这取决于您要寻找哪种UI / UX。

One Dialog with Merged Controllers (arguably the worst approach) 与合并控制器的对话 (可以说是最差的方法)

You can use only one dialog with one template, but you have to merge the controllers together so the new controller can handle both login and sign up logic. 您只能将一个对话框与一个模板一起使用,但是必须将控制器合并在一起,以便新控制器可以处理登录和注册逻辑。

Two Different Dialogs 两种不同的对话框

You can open different ngDialog pop-ups - one for login a different one for sign up. 您可以打开不同的ngDialog弹出窗口-一个用于登录,另一个用于注册。 These would be triggered by different buttons in your view. 这些将由视图中的不同按钮触发。

For example having two buttons (Login and Sign Up) where the Login button would show the Login dialog and the Sign Up button the Sign Up dialog. 例如,有两个按钮(“登录”和“注册”),其中“登录”按钮将显示“登录”对话框,而“注册”按钮将显示“注册”对话框。

Use Components in Your Dialog (arguably the best approach) 在对话框中使用组件 (可以说是最好的方法)

You can open one ngDialog instance and in the template, you could use two different components - one for login and one for sign up. 您可以打开一个ngDialog实例,然后在模板中可以使用两种不同的组件-一种用于登录,另一种用于注册。 Each component would have its own separate template and controller and thus the rule of separation of concerns still applies. 每个组件将具有其自己独立的模板和控制器,因此关注点分离的规则仍然适用。

The added benefit of the last approach is that you can then use these components anywhere on your website - not just the ngDialog template. 最后一种方法的ngDialog好处是您可以在网站上的任何位置使用这些组件,而不仅仅是ngDialog模板。

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

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