javascript - 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] -
i'm building simple directive date countdown. i'm stuck on error
syntax error: token '21' unexpected token @ column 12 of expression [2013-08-28 21:10:14] starting @ [21:10:14]
really don't have clue ho make work
here example on jsfiddle
here coffee script since in javascript it's code :(
.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 = -> = new date() distance = end - 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) ]
you need pass data using model variable not string.
for other issue, please have @ comments:
<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 = 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(); // need refresh ui 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 digest need code $scope.$apply(); above. $timeout does, one-time only. unfortunately, there no corresponding $interval in angularjs. } }; }]); function ctrl($scope){ $scope.t = "2013-08-28 21:10:14"; }
Comments
Post a Comment