在执行ng-repeat和执行在HTML中没有反映的操作时,将作用域从HTML绑定到指令
binding scope from html to the directive while doing ng-repeat and performing action which doesnt reflect back in html
我有一个要求,我需要显示每一个人的计时器倒计时。我写了一个指令,从HTML中获取doi(采访日期),并在指令控制器中执行一些倒计时操作,并每秒更新计时器。下面是html代码:
<div class="panel panel-default" ng-repeat="candidateInfo in aCandidateDetails">
<div class="panel-heading">
<div class="row">
<div class="col-xs-1">
<a style="cursor:pointer">
{{candidateInfo.name}}</a>
</div>
</div>
<div stop-watch time="candidateInfo.doi"></div>
</div>
指令代码:
angular.module('hrPortalApp')
.directive('stopWatch', function() {
debugger;
return {
restrict: 'A',
replace: false,
scope: {
time: "="
},
controller: function($scope) {
debugger;
$scope.getTimeRemaining = function(endtime) {
debugger;
$scope.t = Date.parse(endtime) - Date.parse(new Date());
$scope.seconds = Math.floor(($scope.t / 1000) % 60);
$scope.minutes = Math.floor(($scope.t / 1000 / 60) % 60);
$scope.hours = Math.floor(($scope.t / (1000 * 60 * 60)) % 24);
$scope.days = Math.floor($scope.t / (1000 * 60 * 60 * 24));
return {
'total': $scope.t,
'days': $scope.days,
'hours': $scope.hours,
'minutes': $scope.minutes,
'seconds': $scope.seconds
};
}
$scope.initializeClock = function(endtime) {
debugger;
$scope.clock = document.getElementById('clockdiv');
$scope.daysSpan = $scope.clock.querySelector('.days');
$scope.hoursSpan = $scope.clock.querySelector('.hours');
$scope.minutesSpan = $scope.clock.querySelector('.minutes');
$scope.secondsSpan = $scope.clock.querySelector('.seconds');
$scope.updateClock = function() {
debugger;
$scope.t = $scope.getTimeRemaining(endtime);
$scope.daysSpan.innerHTML = $scope.t.days;
$scope.hoursSpan.innerHTML = ('0' + $scope.t.hours).slice(-2);
$scope.minutesSpan.innerHTML = ('0' + $scope.t.minutes).slice(-2);
$scope.secondsSpan.innerHTML = ('0' + $scope.t.seconds).slice(-2);
if ($scope.t.total <= 0) {
clearInterval($scope.timeinterval);
}
}
$scope.updateClock();
$scope.timeinterval = setInterval($scope.updateClock, 1000);
}
$scope.initializeClock($scope.time);
},
templateUrl: './views/stopWatchView.html'
};
});
完整HTML代码:
<div id="clockdiv">
<div class="tiles">
<span class="days"></span>
<span class="hours"></span>
<span class="minutes"></span>
<span class="seconds"></span>
</div>
<div class="labels">
<li>Days</li>
<li>Hours</li>
<li>Mins</li>
<li>Secs</li>
</div>
的问题,我在这里面临的是每次它更新第一个重复行。它不显示其他行的任何内容。
这可能是由于:
$scope.clock = document.getElementById('clockdiv');
一个ID在一个页面上只出现一次。在ngRepeat的每次传递中,当您想要"a"clockdiv元素时,首先请求"the"clockdiv
元素。
span
的innerHTML
,而是绑定到作用域参数,像这样:
<div class="tiles">
<span class="days" ng-bind="t.days"><!-- this will bind to $scope.t.days --></span>
<span class="hours" ng-bind="t.hours"><!-- this will bind to $scope.t.hours --></span>
<span class="minutes" ng-bind="t.minutes"><!-- this will bind to $scope.t.minutes --></span>
<span class="seconds" ng-bind="t.seconds"><!-- this will bind to $scope.t.seconds --></span>
</div>
嘿!每次更新作用域变量时,您的时钟都会自动更新,并且您不再需要引用代码中的元素。线:
$scope.t = $scope.getTimeRemaining(endtime);
相关文章:
- HTML表单提交时未执行外部函数
- 如何在使用jQuery.html()时防止javascript执行
- HTML 中的 Javascript 不会执行
- 如何在javascript中执行html标记
- 递归单步执行 HTML DOM 并打印属性
- 弹出窗口.js 未在 Chrome 扩展程序中的 Popup 的头部标签中执行.html Popup
- 从命令行执行 HTML Javascript
- 从 Javascript 执行 HTML 代码
- 如何从 Javascript 函数中调用或执行 HTML
- 阻止执行html代码
- 如何仅在单击按钮时执行html代码
- Firefox扩展面板没有't执行html代码
- 为什么在为无线电输入类型执行html post时使用名称" datettype "它没有正确张贴
- 从PHP执行html代码
- JavaScript不执行HTML中的文本框值
- 对img load事件执行html注释
- 防止在回旋镖完成之前执行 html 内容.js
- 在隐藏元素值中执行HTML表单
- 在Android中执行HTML文件中的Javascript
- 自动执行html幻灯片