7

My project needs datetime so I found (Meridian Format in) bootstrap-datetimepicker which seems pretty good, but I am not able to use it

all I did in my code is

  <div class="control-group">
    <label class="control-label">Date</label>

    <div class="controls">
      <div class="control-group input-append date form_datetime" data-date="2012-12-21T15:25:00Z">
        <input size="16" type="text" ng-model="transaction.date" data-date-format="yyyy-mm-dd hh:ii" value="">
        <span class="add-on"><em class="icon-remove"></em></span>
        <span class="add-on"><em class="icon-th"></em></span>
      </div>
    </div>
  </div>

but when I do console.log($scope.transaction) in my controller

  $scope.save = function() {
    var transaction = new Transaction();
    transaction.name = $scope.transaction['name'];
    transaction.debit = $scope.transaction['debit'];
    transaction.date = $scope.transaction['date'];
    transaction.amount = $scope.transaction['amount'];
    transaction.category = $scope.transaction['category'].uuid;
    console.log('adding', $scope.transaction);
 }

I see

Resource {name: "Test", debit: "False", date: undefined, amount: "12", category: "7816635c-3da1-4ccc-b8ab-c07bc3b42202"…}

The date is undefined. How can I associate the value selected in UI with ng-model?

UPDATE
Although, with jQuery, I can see the value

$('.form_datetime input').val()
"08 May 2013 - 08:12 AM"

Thanks

1

3 Answers 3

3

Not all jQuery plugins work right out of the box with Angular. Angular isn't observing the input for "outside" changes, because it expects the value to only change if (a) the user changes it, or (b) it's changed by the controller. Angular code certainly be modified to monitor changes to the value of the input, but performance would likely suffer as a result.

I've replicated the problem you're seeing here: http://jsfiddle.net/kf4Xt/

If you want to get away from your "hack" (pulling the date from .val() manually), you're going to need to wrap this plugin in a directive and use that directive. Doing it this way would be more inline with Angular and you'd be doing it "The Angular Way".

The directive should be responsible for calling $(element).datetimepicker(), and providing the chosen value via a bi-directional binding.

The AngularStrap project is doing exactly this with a lot of the Twitter Bootstrap plugins, so you could check their source how to see how it's being done.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you guide me how to do it? I am pretty new, here is my plunker - plnkr.co/edit/wGrvXAFmPGoYSwh9GfxH?p=preview
Seems like what you have there works, but if you want to take it a step further with an element directive: plnkr.co/edit/uSjf5AfLUTakxVJEsJqB?p=preview -- You'll need to clean it up a bit (use the isolated scope to pass in values so you're not hard-coding everything in the template)
Thanks, Yesterday, I came up with plnkr.co/edit/wGrvXAFmPGoYSwh9GfxH, but your idea is much neat and clean, one question though, how in your plunker I can change ng-model to ng-model="transaction.date", because its failing for me when I do that plus changing scope.ngModel = input.val(); to scope.transaction.date = input.val();. for my case, it has to be part of $scope.transaction = {} which you can see in mu plunker
I have created a question stackoverflow.com/questions/16530722/… for what i have been trying to achieve this
Looks like you got your answer... gotta use Angular's bi-directional binding. :)
|
1

Since, My project also depends upon jQuery, I did the following to make it work in my controller

transaction.date =  $('.form_datetime input').val()

On console.log, I see

Resource {name: "OneMore", debit: "True", date: "2013-05-08T12:27:50", amount: "123", category: "79128f9a-74ab-4c63-b60e-4934aa367575"…

I do not know any better technique at the moment so unblocked myself, if someone knows an angular way, please let me know

Comments

1

I know that this is kind of old, but as long as you have answered yourself with a workaround solution and asked to let you know about an angular way, here is what I think that you were looking for:

Angular Datetime Picker

It is styled with Twitter Bootstrap, but I think that this won't be a problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.