离子倒计时应用程序

3

我正在使用ionic编程一个倒计时应用,不太擅长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>
1个回答

2

如果您查看 $timeout 的文档,您会发现它的第一个参数是一个应在指定延迟之后调用的函数。

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

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

但是它仍然给我错误,并且没有显示应用程序(链接:http://i.imgur.com/68voVXv.png) - Julian Be
@JulianBe:不知道问题出在哪里,可能是应用程序的其他部分(而不是timerCountdown)有问题。看一下这个最小化示例:http://play.ionic.io/app/57ca9c1973c5 - 也许你能看出区别。 - M4N
我有另一个问题,你知道如何显示剩余时间吗? - Julian Be

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接