0

我正在尝试在此链接上ng-idle连接到此工作示例中的外观css及其外观。我已将链接中的代码解释为意味着我应该键入下面的代码来实现它,但下面的代码不会将控制器或样式与视图挂钩。 需要对下面的代码进行哪些特定更改才能成功实现 ng-idle 在上面的演示链接中的外观,带有模式警告和按钮样式等?modal

可以通过单击此文件共享站点的链接下载功能齐全的 MINIMAL 复制品在您的计算机上重新创建这种情况,然后查看当前index.htmlapp.js内容如下:

index.html是:

<html ng-app="demo">
  <head>
    <title title>NgIdle Sample</title> 
  </head>
  <body>

<section data-ng-controller="DemoCtrl">
{{ started }}  <!-- this SYSO of the `start` variable does not print -->
  <p>
    <button type="button" class="btn btn-success" data-ng-hide="started" data-ng-click="start()">Start Demo</button>
    <button type="button" class="btn btn-danger" data-ng-show="started" data-ng-click="stop()">Stop Demo</button>
 </p>
</section>

<script type="text/ng-template" id="warning-dialog.html">
  <div class="modal-header">
   <h3>You're Idle. Do Something!</h3>
  </div>
  <div idle-countdown="countdown" ng-init="countdown=5" class="modal-body">
   <progressbar max="5" value="5" animate="false" class="progress-striped active">You'll be logged out in {{countdown}} second(s).</progressbar>
  </div>

</script>
<script type="text/ng-template" id="timedout-dialog.html">
  <div class="modal-header">
   <h3>You've Timed Out!</h3>
  </div>
  <div class="modal-body">
   <p>
      You were idle too long. Normally you'd be logged out, but in this demo just do anything and you'll be reset.
   </p>
 </div>
</script>

    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/ng-idle/angular-idle.min.js"></script>
    <script src="bower_components/angular-bootstrap/ui-bootstrap.min.js"></script>

    <script src="scripts/app.js"></script>
</body>
</html>

并且app.js是:

'use strict';

angular.module('demo', ['ngIdle', 'ui.bootstrap'])
    .controller('DemoCtrl', function($scope, Idle, Keepalive, $modal){
      $scope.started = false;

      function closeModals() {
        if ($scope.warning) {
          $scope.warning.close();
          $scope.warning = null;
        }

        if ($scope.timedout) {
          $scope.timedout.close();
          $scope.timedout = null;
        }
      }

      $scope.$on('IdleStart', function() {
        closeModals();

        $scope.warning = $modal.open({
          templateUrl: 'warning-dialog.html',
          windowClass: 'modal-danger'
        });
      });

      $scope.$on('IdleEnd', function() {
        closeModals();
      });

      $scope.$on('IdleTimeout', function() {
        closeModals();
        $scope.timedout = $modal.open({
          templateUrl: 'timedout-dialog.html',
          windowClass: 'modal-danger'
        });
      });

      $scope.start = function() {
        closeModals();
        Idle.watch();
        $scope.started = true;
      };

      $scope.stop = function() {
        closeModals();
        Idle.unwatch();
        $scope.started = false;

      };
    })
    .config(function(IdleProvider, KeepaliveProvider) {
      IdleProvider.idle(5);
      IdleProvider.timeout(5);
      KeepaliveProvider.interval(10);
    })
 .run(['Idle', function(Idle) {
      Idle.watch();
    }]);

现在重新创建了问题,需要对上面的代码进行哪些更改以使其实现模态和样式,如 OP 顶部链接中的示例/演示中所示?

4

1 回答 1

1

在你 TARed 的项目中,有四件事:

  1. 您需要确保ng-app="demo"将属性添加到htmlorbody元素。demo当您在以下行中定义应用程序模块时,应更改该值以匹配您调用应用程序的任何内容app.jsangular.module('demo', [/* etc. */])
  2. 确保你包括ui-bootstrap-tpls.min.js而不是ui-bootstrap.min.js;后者不包含指令的 HTML 模板(期望您自己提供它们),而前者包含。请参阅 UI-Bootstrap入门指南
  3. 您引用的 UI-Bootstrap 版本比我编写演示时使用的版本更新。$modal已重命名为$uibModal,因此只需搜索并替换该令牌。
  4. 确保在应用程序的 CSS 中包含 Bootstrap。您使用 Yeoman 生成器启动了这个应用程序,该生成器似乎将 Bootstrap SASS 编译为styles/main.css.

其中一些东西(1,2 和 4)可能已经由 Yeoman 生成器为您设置好了,而您尝试将其破解并粘贴到 ng-idle 演示代码中。

这是您的 index.html (删除了所有已注释掉的内容):

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" href="styles/main.css">
  <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet">
</head>

<body ng-app="demo">
  <!--[if lte IE 8]
  <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
  [endif]//-->

  <div class="header">
    <div class="navbar navbar-default" role="navigation">
      <div class="container">
        <div class="navbar-header">

          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#js-navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>

          <a class="navbar-brand" href="#/">client</a>
        </div>

        <div class="collapse navbar-collapse" id="js-navbar-collapse">

          <ul class="nav navbar-nav">
            <li class="active"><a href="#/">Home</a></li>
            <li><a ng-href="#/about">About</a></li>
            <li><a ng-href="#/">Contact</a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>

  <div class="container">
    <div ng-view="">
      <section data-ng-controller="DemoCtrl">
        {{ started }}
        <!-- this SYSO of the `start` variable does not print -->
        <p>
          <button type="button" class="btn btn-success" data-ng-hide="started" data-ng-click="start()">Start Demo</button>
          <button type="button" class="btn btn-danger" data-ng-show="started" data-ng-click="stop()">Stop Demo</button>
        </p>
      </section>
    </div>
  </div>

  <div class="footer">
    <div class="container">
      <p><i class="fa fa-heart"></i> from the Yeoman team</p>
    </div>
  </div>

  <script type="text/ng-template" id="warning-dialog.html">
    <div class="modal-header">
      <h3>You&apos;re Idle. Do Something!</h3>
    </div>
    <div idle-countdown="countdown" ng-init="countdown=5" class="modal-body">
      <progressbar max="5" value="5" animate="false" class="progress-striped active">You&apos;ll be logged out in {{countdown}} second(s).</progressbar>
    </div>

  </script>
  <script type="text/ng-template" id="timedout-dialog.html">
    <div class="modal-header">
      <h3>You&apos;ve Timed Out!</h3>
    </div>
    <div class="modal-body">
      <p>
        You were idle too long. Normally you&apos;d be logged out, but in this demo just do anything and you&apos;ll be reset.
      </p>
    </div>
  </script>

  <script src="bower_components/jquery/dist/jquery.js"></script>
  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js"></script>
  <script src="bower_components/angular-animate/angular-animate.js"></script>
  <script src="bower_components/angular-aria/angular-aria.js"></script>
  <script src="bower_components/angular-cookies/angular-cookies.js"></script>
  <script src="bower_components/angular-messages/angular-messages.js"></script>
  <script src="bower_components/angular-resource/angular-resource.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
  <script src="bower_components/angular-touch/angular-touch.js"></script>
  <script src="bower_components/ng-idle/angular-idle.min.js"></script>
  <script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>

  <script src="scripts/app.js"></script>
  <script src="scripts/controllers/main.js"></script>

</body>

</html>

这是app.js

  'use strict';
  angular.module('demo', ['ngIdle', 'ui.bootstrap'])
    .controller('DemoCtrl', function($scope, Idle, Keepalive, $uibModal){
      $scope.started = true;

      function closeModals() {
        if ($scope.warning) {
          $scope.warning.close();
          $scope.warning = null;
        }

        if ($scope.timedout) {
          $scope.timedout.close();
          $scope.timedout = null;
        }
      }

      $scope.$on('IdleStart', function() {
        closeModals();

        $scope.warning = $uibModal.open({
          templateUrl: 'warning-dialog.html',
          windowClass: 'modal-danger'
        });
      });

      $scope.$on('IdleEnd', function() {
        closeModals();
      });

      $scope.$on('IdleTimeout', function() {
        closeModals();
        $scope.timedout = $uibModal.open({
          templateUrl: 'timedout-dialog.html',
          windowClass: 'modal-danger'
        });
      });

      $scope.start = function() {
        closeModals();
        Idle.watch();
        $scope.started = true;
      };

      $scope.stop = function() {
        closeModals();
        Idle.unwatch();
        $scope.started = false;

      };
    })
    .config(function(IdleProvider, KeepaliveProvider) {
      IdleProvider.idle(5);
      IdleProvider.timeout(5);
      KeepaliveProvider.interval(10);
    })
    .run(['Idle', function(Idle) {
      Idle.watch();
    }]);

我尊重学习事情如何工作的实践方法,但我建议你稍微踩一下刹车,了解 Angular、你的 yeoman 生成器、Bootstrap、UI-Bootstrap 和 ng-idle 之间的关系,以及如何使用这些东西。

于 2016-02-19T13:55:34.227 回答