【问题标题】:Timer binding not working ionic定时器绑定不起作用离子
【发布时间】:2015-12-14 22:17:58
【问题描述】:

我已经创建了一个秒表工厂服务,除了运行之外并没有做太多的事情(重置和其他功能没有实现,忽略它们)。

我设置了一个$scope.time 来接收计时器的变化,但是它没有被更新。

我尝试了许多不同的方法,我认为最后一种方法确实很糟糕,但我想尝试让它发挥作用。

我的看法:

<ion-view view-title="Clock">
  <ion-content>
    <div ng-controller="controller as ClockCtrl">
      <h1 ng-bind="ClockCtrl.time"></h1>
    </div>
  </ion-content>
</ion-view>

我的控制器:

.controller('ClockCtrl', function($scope, Stopwatch) {
  var vm = $scope;
  $scope.time = "";
  Stopwatch.init($scope);
})

我的服务:

factory('Stopwatch', function() {
  var Stopwatch = (function() {
                var s;
                var isRunning = 0;
                var time = 0;
        var thatElement;
                function run(element){
                    if(isRunning !== 0){
                        time++;
                        s.mills = time % 10;
                        s.secs = Math.floor(time / 10);
                        s.mins = Math.floor(time / 10 / 60);
                        //Stopwatch.currentTime = ("0"+s.mins).slice(-2)+":"+("0"+s.secs).slice(-2)+":"+("0"+s.mills).slice(-2);
            thatElement.time = ("0"+s.mins).slice(-2)+":"+("0"+s.secs).slice(-2)+":"+("0"+s.mills).slice(-2);
                        setTimeout(run, 100);
                    }
                };

                return {
                    settings: {
                        currTime: "",
                        mills: 0,
                        secs: 0,
                        mins: 0,
                        i: 1,
                        times: ["00:00:00"],
                    },
                    currentTime: "",
                    init: function(element) {
                        thatElement = element;
                        s = this.settings;
                        this.start(element);
                    },
                    clear: function() {
                        s.i = 1,
                        s.mins = s.secs = s.mills = 0;
                        s.times = ["00:00:00"],
                        s.results.innerHTML = s.clearButton;
                    },
                    start:function(element){
                        isRunning = 1;
                        run(element);
                    },
                    stop: function(){
                        isRunning = 0;
                    }
                }

            })();
  return Stopwatch;
})

我知道最后一种方法不好,我会接受任何指针,这是我第一次使用 ionic,第二次尝试 angular。

提前致谢!

【问题讨论】:

  • 您应该使用自定义过滤器来确定时间格式。这是一个很好的例子,用 $timeout 和 $interval 来解释。加上定制的过滤器。运行 sn-p 看看它是如何工作的 stackoverflow.com/questions/32285679/…
  • 感谢您的参考,但在该示例中,代码位于控制器内部,我希望从工厂获取它

标签: javascript angularjs binding ionic-framework ionic


【解决方案1】:

由于您使用的是controllerAs 语法,因此您需要设置控制器实例属性,而不是$scope 对象一:

.controller('ClockCtrl', function($scope, Stopwatch) {
  this.time = "";
  Stopwatch.init(this);
});

【讨论】:

  • 不幸的是,更改代码并没有改变结果。还有其他想法吗?当我调试时,我看到 vm.time 正在改变,但绑定没有(和以前一样)。
  • 你还需要将this而不是$scope传递给Stopwatch.init
  • 是的,当我看到你的代码时我也试过了。运气不好:-\
猜你喜欢
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 2017-09-26
  • 2018-11-20
  • 2018-08-28
  • 1970-01-01
相关资源
最近更新 更多