简体   繁体   English

角度ui路线隐藏模式

[英]Angular ui route hide modal

How can I close a modal, with ng-click on angular ui route? 如何在Angular ui路径上ng-click来关闭模态?

I have this HTML file with UI route angular: 我有UI路由成角度的HTML文件:

 <div ui-view="modalView"></div>
 <div ui-sref="openModal">Open Modal</div>

And this is my config: 这是我的配置:

$stateProvider.state('openModal', {
views: {
 'modalView': { 

    templateUrl: "/partials/Modal.html"

}
     }

And then in my Modal.html I have: 然后在我的Modal.html中,我有:

 <div style ="position:fixed; width:100%; heigth:100%; background-color:black;">
     //i want to click in this div and close the modal
            <div style = "position:relative; float:right;"><i class="fa fa-times fa" aria-hidden="true"></i></div>

 </div>

How can I do this without using jquery? 不使用jquery怎么办?

In your template file Modal.html add a click event 在您的模板文件Modal.html中添加点击事件

 <div style ="position:fixed; width:100%; heigth:100%; background-color:black;" [ngClass]="{'hide-class': highlightedDiv === 1 }>
    <div style = "position:relative; float:right;" (click)="toggleHighlight(1);"><i class="fa fa-times fa" aria-hidden="true"></i></div>
 </div>

In your component file add the function toggleHighlight 在您的组件文件中添加功能toggleHighlight

toggleHighlight(newValue: number) {
  if (this.highlightedDiv !== newValue) {
    this.highlightedDiv = newValue;
  }
}

And finally in CSS add 最后在CSS中添加

.hide-class { display: none; }

This will probably solve your issue 这可能会解决您的问题

Since you are using router, You may be looking for a solution using router. 由于您使用的是路由器,因此您可能正在寻找使用路由器的解决方案。 You can listen to router's $stateChangeSuccess event to save previous state and transition to it when user click the div to close the modal: 您可以侦听路由器的$ stateChangeSuccess事件,以保存先前的状态并在用户单击div关闭模式时过渡到该状态:

Listen to the $stateChangeSucces event in the app root controller: 在应用程序根控制器中监听$ stateChangeSucces事件:

app.controller('AppController', function($state, $rootScope) {      
    $rootScope.previousState;       
    $rootScope.$on('$stateChangeSuccess', function(ev, to, toParams, from, fromParams) {
        $rootScope.previousState = from.name;
        console.log("prev state: ", $rootScope.previousState);
    });
});

In the controller of your modal, you intercept the click and go to the previously saved previous state : 在模态控制器中,您将拦截点击并转到先前保存的先前状态:

app.controller('ModalController', function($state, $scope, $rootScope) {
    $scope.closeModal = function() {
        $state.transitionTo($rootScope.previousState || 'root');
    };
});         

Your modal's template should looks like: 您的模态模板应如下所示:

<div style ="position:fixed; width:100%; heigth:100%; background-color:black;">
    <div ng-click="closeModal() "style = "position:relative; float:right;"><i class="fa fa-times fa" aria-hidden="true"></i></div>
</div>

Don't forget to associate the ModalController and modal template when setting openModal state 设置openModal状态时,请不要忘记将ModalController和模态模板相关联

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

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