【问题标题】:window.close how to close the current window in angularwindow.close 如何以角度关闭当前窗口
【发布时间】:2016-06-30 07:50:45
【问题描述】:

嗨,这是我当前的代码,一旦我取消弹出窗口,我必须关闭整个应用程序选项卡,这意味着窗口

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

    <script>
      angular.module('plunker', ['ui.bootstrap']);
      var ModalDemoCtrl = function ($scope, $modal, $log) {

          /* Note: The hard coded user object has been commented out, as 
             it is now passed as an object parameter from the HTML template.
          */
          /* $scope.user = {
              user: 'name',
              password: null
          };*/

          $scope.open = function (user) {
              $scope.user = user;
              $modal.open({
                  templateUrl: 'myModalContent.html',
                  backdrop: true,
                  windowClass: 'modal',
                  controller: function ($scope, $modalInstance, $log, user) {
                      $scope.user = user;
                      $scope.submit = function () {
                          $log.log('Submiting user info.');
                          $log.log(JSON.stringify(user));
                          $modalInstance.dismiss('cancel');
                      }
                      $scope.cancel = function () {
                          $modalInstance.dismiss('cancel');
                         window.open('location', '_self', '');
                        window.close();
                      };
                  },
                  resolve: {
                      user: function () {
                          return $scope.user;
                      }
                  }
              });
          };
      };  
  </script>

  <div ng-controller="ModalDemoCtrl">
      <script type="text/ng-template" id="myModalContent.html">
          <div class="modal-header">
              <h3>I'm a modal!</h3>
          </div>
          <form ng-submit="submit()">
            <div class="modal-body">
              <label>User name</label>
              <input type="text" ng-model="user.user" />
              <label>Password</label>
              <input type="password" ng-model="user.password" />
            </div>
            <div class="modal-footer">
                <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
                <input type="submit" class="btn primary-btn" value="Submit" />
            </div>
          </form>
      </script>

      <button class="btn" ng-click="open({user: 'username', password: 'password'})">Open me!</button>
      <div ng-show="user.password">Got back from modal: {{ user | json}}</div>
  </div>
  </body>
</html>

此代码不起作用请提供任何帮助

window.close() 不工作

【问题讨论】:

  • 来看看我的问题,我说需要关闭窗口而不是弹出窗口
  • 它会抛出什么错误?
  • 我给我的代码检查,没有错误
  • 你能试试$window.close()吗?

标签: javascript angularjs


【解决方案1】:

window.close 只会关闭应用程序打开的窗口,而不是用户打开的窗口/选项卡。这意味着当用户访问您的网站时,他/她打开了窗口而不是您的应用程序。但如果你使用window.open 使用javascript 打开一个新窗口,你可以使用window.close 关闭。这是出于安全原因,几乎所有最新的浏览器都内置了这一点。

【讨论】:

  • 在我尝试它不起作用的答案之前检查我的代码
  • 以上解决方案不是答案
  • 我检查过了。这就是我所说的,您正在用户使用“_self”打开的同一窗口中打开新窗口。您的应用未打开该窗口。
  • 你能用我上面的代码为我的问题创建一个小提琴
【解决方案2】:

$window 服务中使用$window.close()

您在询问角度模态。 用这个

$modalInstance.dismiss('cancel'); or $modalInstance.close();

阅读本文以获取更多信息modal

【讨论】:

    猜你喜欢
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    相关资源
    最近更新 更多