Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically binding countdown value #23

Closed
wisemanIV opened this issue Oct 3, 2013 · 11 comments
Closed

Dynamically binding countdown value #23

wisemanIV opened this issue Oct 3, 2013 · 11 comments

Comments

@wisemanIV
Copy link

Great work. How do I dynamically bind the countdown value from data retrieved from a remote REST service and start the countdown once the data is retrieved?

@siddii
Copy link
Owner

siddii commented Oct 3, 2013

Sounds similar to this - #21 (comment)

@wisemanIV
Copy link
Author

My initial attempts to use the methods in #21 failed but I need to spend more time on it to figureout where I'm going wrong. My scenario is retrieving data from a REST service. I hacked the time app.js to get a working version - https://gist.github.com/wisemanIV/6818132

@vpixle
Copy link

vpixle commented Oct 5, 2013

Well, "to retrieve countdown value from a remote REST" is kind a niche request that shouldn't be solved inside the directive. I have just solved my issue by just adding a timespan attr to the directive to be able to construct and calculate the from - to countdown. The REST should be done in some service with other data manipulation.

Your scope part of the directive should look like this
scope: { interval: '=interval', startTimeAttr: '=startTime', countdownattr: '=countdown', timespan: '=timespan' // <===== autoStart: '&autoStart' },

@davemerwin
Copy link

You also should be able to do ng-attr- appended to the countdown attr and it "should" work. So the dynamic value from the model should be rendered. But it keeps throwing an error.

Error: [$parse:syntax] Syntax Error: Token 'tweaked' is unexpected, expecting [:] at column 4 of the expression [{$ tweaked $}] starting at [tweaked $}].

More about ng-attr- : http://docs.angularjs.org/guide/directive

Any thoughts?

@siddii
Copy link
Owner

siddii commented Dec 31, 2013

@davemerwin - Here is an example using ng-attr- syntax...

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <script src="http://siddii.github.io/angular-timer/bower_components/angular/angular.min.js"></script>
    <script src="http://siddii.github.io/angular-timer/dist/angular-timer.min.js"></script>
    <script>
        angular.module('timer').controller('timerDemoController',function ($scope) {
            console.log('##### $scope = ', $scope);
            $scope.countdown = 50;
            $scope.interval = 1000;
        });
    </script>
</head>
<body ng-app="timer">
<h3>Timer Countdown with ng-attr</h3>

<div ng-controller="timerDemoController">
    <timer ng-attr-countdown="countdown" ng-attr-interval="interval">{{countdown}}</timer>
</div>

</body>

@davemerwin
Copy link

This worked great as well. Thanks for the help!

@johnparn
Copy link
Contributor

johnparn commented Feb 5, 2014

Hi!

Setting the inital value for countdown works fine. However, how do I change the value after it has been set?

I've put up a plunker that hopefully demonstrates the issue, in this case a button that would reset the countdown value.

http://embed.plnkr.co/FAjCBGGNHo9Ne5QQJ9DF/preview

Seems like a link is needed in the directive, something like this (needs of course a bit more elaboration):

      link: function(scope, iElement, iAttrs, ctrl) {

        scope.$watch('countdownattr', function(newVal, oldVal){
            if (scope.countdown) {
                scope.countdown = 120;
            }
        });

      }

Update: solved it by using a listener instead. See piull request #44.

johnparn added a commit to johnparn/angular-timer that referenced this issue Feb 5, 2014
With the listener one can set the countdown value using
$broadcast('countdown-set', value). This might be an edge case, but I
need to set a new countdown. This makes the countdown value dynamic set.
Issue siddii#23 describes how to set the value on initialization but using
this one can change the value whenever necessary.
@311chaos
Copy link

311chaos commented Feb 6, 2014

Can you update your plunkr with the working reset button? I am having a similar issue and I am not fully understanding how to use the listener.

@johnparn
Copy link
Contributor

johnparn commented Feb 6, 2014

@311chaos : I put up a new plnkr showing how to use broadcast to change the value of countdown.

http://embed.plnkr.co/vYfICUKrpwG62p1dHHEY/preview

In the example above I've also included a fix so that the last emitted millisecond value is 0 not the value just right before. Check the console. In the original code, if you have a countdown with an interval of 1000, the last emitted millis value is 1000. I wanted the last value to be 0. I'll perhaps make PR for that.

johnparn added a commit to johnparn/angular-timer that referenced this issue Feb 7, 2014
With the listener one can set the countdown value using
$broadcast('countdown-set', value). This might be an edge case, but I
need to set a new countdown. Issue siddii#23 describes how to set the value on
initialization but using this one can change the value whenever
necessary.

Also changed the order of execution of some code to make 0 the last
emitted millis value for a countdown. In the original code the last emit
message was the value just
before 0. For example with a countdown with an interval of 1000 the last
emitted millis value was 1000.
@siddii siddii closed this as completed Mar 18, 2014
@beinguideveloper
Copy link

how do i bind start-time as below is my code

My HTML Code:

{{minutes}}:{{seconds}}  mins over
                 </div>

My JS Code :

$scope.startTimerVal = ($scope.startHour_360000)+($scope.startMin_60000)+($scope.startSec*1000);

In startTimerVal I get dynamic value where I convert that value into miliseconds

Using this I get below error

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{startTimerVal}}] starting at [{startTimerVal}}].

@beinguideveloper
Copy link

My HTML Code:

[timerautostart="false"start-time="{{startTimerVal}}"]{{minutes}}:{{seconds}}[/timer]

i am using square brackets instead of greater than and less than sign to show my demo code as those sign wont works here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants