简体   繁体   English

绑定Angular服务以进行查看

[英]Binding an Angular service to view

I created a countdown clock as part of a larger project. 我创建了一个倒数时钟,作为一个较大项目的一部分。 Here is the code for the service 这是服务的代码

'use strict';

angular.module('myApp')
    .service('Countdownclock', function Countdownclock() {
        var secondsRemaining = 0;
        var timerProcess;
        return {
            //initialize the clock
            startClock: function(minutes) {
                secondsRemaining = minutes * 60;
                timerProcess = setInterval(this.timer, 1000);
            },
            //timer function
            timer: function() {
                secondsRemaining -= 1;
                if (secondsRemaining <= 0) {
                    clearInterval(timerProcess);
                }
            },
            //get time
            getTime: function() {
                return secondsRemaining;
            },
            //add time
            addTime: function(seconds) {
                secondsRemaining += seconds;
            },
            //stop clock
            stopClock: function() {
                secondsRemaining = 0;
                clearInterval(timerProcess);
            }
        };
    });

Then I call it to a from a controller which is also linked to a view 然后我从也链接到视图的控制器中将其称为

'use strict';

angular.module('myApp')
    .controller('MainCtrl', function($scope, Countdownclock) {
        Countdownclock.startClock(1);
        $scope.seconds = Countdownclock.getTime();
        $scope.$watch(Countdownclock.getTime(), function(seconds) {
            $scope.seconds = Countdownclock.getTime();
        });
    });

For some reason I can't figure out how to bind secondsRemaining to $scope.seconds. 由于某种原因,我不知道如何将secondsRemaining绑定到$ scope.seconds。 I've been trying to figure this thing out for about an hour. 我一直在努力解决这个问题大约一个小时。 I'm not exactly a wiz at functional programing so I have a feeling I'm just thinking about it wrong. 我并不是函数编程的专家,所以我有一种想法是错误的。

Inject $interval into your service and replace setInterval with it: $interval注入您的服务,并用它替换setInterval

timerProcess = $interval(this.timer, 1000);

If you want to use a watcher you can register it like this: 如果您想使用观察者,可以像这样注册:

$scope.$watch(function () { return Countdownclock.getTime(); }, function (newValue, oldValue) {
  // Might be identical when called due to initialization - Good to know for some cases
  if (newValue !== oldValue) { 
    $scope.seconds = newValue;
  }
});

Demo : http://plnkr.co/edit/usUoOtWMwoDRht27joOA?p=preview 演示http : //plnkr.co/edit/usUoOtWMwoDRht27joOA?p=preview

You can use a function instead: 您可以改用一个函数:

$scope.seconds = function() { return Countdownclock.getTime() };

Then remove the 然后删除

$scope.$watch(Countdownclock.getTime(), function(seconds) {
    $scope.seconds = Countdownclock.getTime();
});

You can then use it in your template like this: 然后可以在模板中使用它,如下所示:

<div>{{seconds()}}</div>

But first, like Spock said, you have to use $interval instead of setInterval. 但是首先,就像Spock所说的那样,您必须使用$ interval而不是setInterval。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM