我正在构建日期倒计时的简单指令。但我坚持这个错误
Syntax Error: Token '21' is an unexpected token at column 12 of the expression [2013-08-28 21:10:14] starting at [21:10:14]
真的没有线索让它发挥作用
这是我在jsfiddle上的例子
这是咖啡脚本,因为在javascript中代码太多了:(
.directive "timer", ["$compile", ($compile) ->
restrict: "E"
replace: false
scope:
endTimeAttr: "=endTime"
controller: ($scope, $element) ->
_second = 1000
_minute = _second * 60
_hour = _minute * 60
_day = _hour * 24
timer = undefined
showRemaining = ->
now = new Date()
distance = end - now
if distance < 0
clearInterval timer
setExpired "EXPIRED!"
return
$scope.days = Math.floor(distance / _day)
$scope.hours = Math.floor((distance % _day) / _hour)
$scope.minutes = Math.floor((distance % _hour) / _minute)
$scope.seconds = Math.floor((distance % _minute) / _second)
setExpired = (value) ->
content = angular.element("<div></div>").html(value).contents()
compiled = $compile(content)
element.html ""
element.append content
compiled scope
end = new Date($scope.endTime)
timer = setInterval(showRemaining, 1000)
]
答案 0 :(得分:3)
您需要使用模型变量而不是字符串传递数据。
关于其他问题,请查看评论:
<div ng-init="testapp" ng-controller="ctrl">
<timer end-time="t">{{hours}} hours, {{minutes}} minutes, {{seconds}} seconds</timer>
</div>
app = angular.module('testapp', [])
app.directive('timer', ['$compile', function ($compile) {
return {
restrict: 'E',
replace: false,
scope: {
endTimeAttr: '=endTime'
},
controller: function ($scope, $element) {
var end = new Date($scope.endTimeAttr); //use endTimeAttr instead of endTime
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
setExpired('EXPIRED!');
return;
}
$scope.days = Math.floor(distance / _day);
$scope.hours = Math.floor((distance % _day) / _hour);
$scope.minutes = Math.floor((distance % _hour) / _minute);
$scope.seconds = Math.floor((distance % _minute) / _second);
$scope.$apply(); // you need this to refresh the UI by calling $digest
}
function setExpired(valur) {
var content = angular.element('<div></div>').html(value).contents();
var compiled = $compile(content);
element.html('');
element.append(content);
compiled(scope)
}
timer = setInterval(showRemaining, 1000); //doesn't do digest so you need the code $scope.$apply(); above. $timeout does, but it is for one-time only. Unfortunately, there is no corresponding $interval in AngularJS.
}
};
}]);
function ctrl($scope){
$scope.t = "2013-08-28 21:10:14";
}
的 Working Demo 强>