【问题标题】:Ionic Countdown Timer App离子倒数计时器应用程序
【发布时间】:2016-04-02 16:45:40
【问题描述】:

我正在用 ionic 编写一个 Countdown 应用程序,我不擅长 AngularJS,因此,我不知道我做错了什么:/ 或许你可以看看我的代码,应该有一个按钮,开始倒计时,如果倒计时结束,应该会弹出一条消息。

timer.js

    angular.module('PopupTimer', [])

.controller('PopupCtrl',function($scope, $ionicPopup, $timeout) {

    $scope.showAlert = function() {
     var alertPopup = $ionicPopup.alert({
       title: 'Your Pizza Is Ready!',
       template: 'Bon Appétit!'
     });

     alertPopup.then(function(res) {
       console.log('Pizza Is Ready!');
     });
   };

    $scope.timerCountdown = function(res){
      var alertPopup = $ionicPopup.alert({
       title: 'Your Pizza Is Ready!',
       template: 'Bon Appétit!'});

      var endtimer = $timeout([200]);

      if(endtimer == ([0])) {
       return showAlert();
      }
  }
});

app.js

    // Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

  // Don't remove this line unless you know what you are doing. It stops the viewport
  // from snapping when text inputs are focused. Ionic handles this internally for
  // a much nicer keyboard experience.
  cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}


 });
})

index.html

    <!DOCTYPE html>
<html ng-app="PopupTimer">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/timer.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar align-title="center" class="bar-assertive">
        <h1 class="title">Pizza Timer</h1>
      </ion-header-bar>
      <ion-content ng-controller="PopupCtrl">
        <button class="button button-full button-light" ng-click="timerCountdown">
          Alert
        </button>
      </ion-content>    
    </ion-pane>
  </body>
</html>

【问题讨论】:

    标签: javascript angularjs ionic


    【解决方案1】:

    如果你看documentation of $timeout,你会发现它的第一个参数是一个函数,应该在指定的延迟后调用。

    延迟(第二个参数)以毫秒为单位指定。因此,要在 5 秒后显示警报,您的 $sope.timerCountdown 函数将如下所示:

    $scope.timerCountdown = function(res){
      var endtimer = $timeout(
        function() {
          var alertPopup = $ionicPopup.alert({
           title: 'Your Pizza Is Ready!',
           template: 'Bon Appétit!'});
        },
        5000);
    };
    

    【讨论】:

    • 但它仍然给我错误并且不显示应用程序(链接:i.imgur.com/68voVXv.png
    • @JulianBe:不知道问题出在哪里,肯定是在应用程序的另一部分(不是在 timerCountdown 中)。看看这个最小的例子:play.ionic.io/app/57ca9c1973c5 - 也许你可以看到区别。
    • 我还有一个问题,你知道我怎么显示还剩多少时间吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多