Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将角度服务绑定到视图_Javascript_Angularjs_Service_Model Binding - Fatal编程技术网

Javascript 将角度服务绑定到视图

Javascript 将角度服务绑定到视图,javascript,angularjs,service,model-binding,Javascript,Angularjs,Service,Model Binding,作为一个更大项目的一部分,我创建了一个倒计时钟。这是服务的代码 'use strict'; angular.module('myApp') .service('Countdownclock', function Countdownclock() { var secondsRemaining = 0; var timerProcess; return { //initialize the clock

作为一个更大项目的一部分,我创建了一个倒计时钟。这是服务的代码

'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);
            }
        };
    });

由于某些原因,我无法理解如何将secondsRemaining绑定到$scope.seconds。我想弄清楚这件事已经有一个小时了。我不是一个真正的功能编程高手,所以我感觉我只是想错了。

$interval
注入到您的服务中,并用它替换
setInterval

timerProcess = $interval(this.timer, 1000);
如果您想使用watcher,可以这样注册:

$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;
  }
});
<div>{{seconds()}}</div>

演示

向您的服务中注入
$interval
,并用它替换
setInterval

timerProcess = $interval(this.timer, 1000);
如果您想使用watcher,可以这样注册:

$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;
  }
});
<div>{{seconds()}}</div>

演示

向您的服务中注入
$interval
,并用它替换
setInterval

timerProcess = $interval(this.timer, 1000);
如果您想使用watcher,可以这样注册:

$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;
  }
});
<div>{{seconds()}}</div>

演示

向您的服务中注入
$interval
,并用它替换
setInterval

timerProcess = $interval(this.timer, 1000);
如果您想使用watcher,可以这样注册:

$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;
  }
});
<div>{{seconds()}}</div>

演示

您可以改用函数:

$scope.seconds = function() { return Countdownclock.getTime() };
然后取下盖子

$scope.$watch(Countdownclock.getTime(), function(seconds) {
    $scope.seconds = Countdownclock.getTime();
});
然后,您可以在模板中使用它,如下所示:

$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;
  }
});
<div>{{seconds()}}</div>
{{seconds()}

但首先,正如斯波克所说,您必须使用$interval而不是setInterval。

您可以使用函数:

$scope.seconds = function() { return Countdownclock.getTime() };
然后取下盖子

$scope.$watch(Countdownclock.getTime(), function(seconds) {
    $scope.seconds = Countdownclock.getTime();
});
然后,您可以在模板中使用它,如下所示:

$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;
  }
});
<div>{{seconds()}}</div>
{{seconds()}

但首先,正如斯波克所说,您必须使用$interval而不是setInterval。

您可以使用函数:

$scope.seconds = function() { return Countdownclock.getTime() };
然后取下盖子

$scope.$watch(Countdownclock.getTime(), function(seconds) {
    $scope.seconds = Countdownclock.getTime();
});
然后,您可以在模板中使用它,如下所示:

$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;
  }
});
<div>{{seconds()}}</div>
{{seconds()}

但首先,正如斯波克所说,您必须使用$interval而不是setInterval。

您可以使用函数:

$scope.seconds = function() { return Countdownclock.getTime() };
然后取下盖子

$scope.$watch(Countdownclock.getTime(), function(seconds) {
    $scope.seconds = Countdownclock.getTime();
});
然后,您可以在模板中使用它,如下所示:

$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;
  }
});
<div>{{seconds()}}</div>
{{seconds()}

但首先,正如斯波克所说,您必须使用$interval而不是setInterval。

您不应该使用setInterval或clearInterval方法。使用角度等效项。抱歉,现在移动设备限制了我的写作。稍后将尝试查看try$scope。$watch('seconds',……您不应该使用setInterval或clearInterval方法。请使用角度等效方法。很抱歉,现在移动设备限制了我的写作。稍后将尝试查看try$scope。$watch('seconds',…您不应该使用setInterval或clearInterval方法。请使用角度等效方法。抱歉,现在移动设备限制了我的写作。稍后将尝试查看try$scope。$watch('seconds',..…您不应该使用setInterval或clearInterval方法。请使用角度等效方法。很抱歉,现在移动设备限制了我的写作。稍后将尝试查看try$scope。$watch('seconds',..),。。。。。