【问题标题】:opening a modal without using a button in angularJS which is accessible to all the controllers在不使用 angularJS 中的按钮的情况下打开模式,所有控制器都可以访问
【发布时间】:2014-08-05 14:00:05
【问题描述】:
 <div class="modal-content">
    <div class="modal-header">
        <h3 class="modal-title">Standard message modal</h3>
    </div>

    <div class="modal-body">

    </div>
    <div class="modal-footer">
        <button class="btn btn-default">Yes</button>
        <button class="btn btn-default">No</button>
    </div>
</div>

我是 angularJS 的新手,我希望在遇到警报语句时打开模式。基本上使用模态来提醒而不是提醒框

【问题讨论】:

    标签: angularjs typescript angularjs-controller angularjs-model


    【解决方案1】:

    我不确定您是否可以覆盖浏览器对警报的处理。您至少需要使用注入的工厂来更改(查找和替换)您的警报代码...

    这里的好处是您可以将 modalFactory 注入您需要从(可能是所有这些)引发模式的任何控制器中并一致地重用该代码。虽然它没有回答您覆盖 Alert 的问题,但它确实为您提供了一种以编程方式显示模态元素的方法。这是 plunk http://plnkr.co/edit/0QNxUrQ7DH101VrFD0Me?p=preview 希望对您有所帮助。

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope, modalFactory) {
      $scope.name = 'ttt';
    
      $scope.RunProcess =  function() {
        //error occured
        modalFactory.show($scope);
      }
    
      $scope.Reset =  function() {
        modalFactory.hide($scope);
      }
    
    });
    
    app.factory('modalFactory', function() {
      return {
        show: function($scope) {
          $scope.showModal = true;
        },
        hide: function($scope) {
          $scope.showModal = true;
        }
      }
    });
    

    那么在你看来

     <body ng-controller="MainCtrl">
      <div class="my-modal" ng-show="showModal">
        blahhh
      </div>
        <p>Hello {{name}}!</p>
        <div>
          <button ng-click="RunProcess()">button that runs a controller method and throws alert</button>
        </div>
      </body>
    

    【讨论】:

      【解决方案2】:

      只需使用 Angular-Strap 之类的东西:http://mgcrea.github.io/angular-strap/##modals

      我不推荐它。但是如果你想使用自定义代码,你可以覆盖警报:

      window.alert = function(msg){ console.log(msg) }; 
      alert('foo'); // logs to console
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-23
        • 2016-06-04
        • 2021-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        相关资源
        最近更新 更多