0

I've the following controller in AngularJS:

$scope.model = null;
$scope.load = function() {
     $http.get(someUrl).success(function (data) {
                $scope.model = data;
     }
}

$scope.save = function() {
      $http.post(url, $scope.model)
           .success(function (data) {
                // some handle
            }).error(function () {
                 // some handle
            });
}

The model scheme:

$scope.model.PayDate === "2015-04-29T22:00:00.000Z"
// deserialized from request, date is in ASP.NET format

Next, the PayDate prop is attached to some control (via ng-model), that sets the new date in javascript DateTime class. So, the value of changed PayDate before sending to the server looks like:

value

Unfortunately, when request is sent to the server, the value of PayDate is the old one:

val

Why the old value of PayDate is sent instead of current stored in the model?

EDIT: The workaround is to convert JS date to string before send:

$scope.model.PayDate = moment($scope.model.PayDate ).format("YYYY-MM-DD");
$scope.save();

Then, new value is send in a request.

Of course, I would like to fix the issue instead of converting each date field into string just before posting.

  • 1
    Is the control creating a child scope? – Mario Levrero Apr 21 '15 at 13:38
  • How can I check that? I use https://github.com/dalelotts/angular-bootstrap-datetimepicker as date time picker. –  Apr 21 '15 at 13:40
  • I've had problems due to timezone stuff before. Not sure if this is a similar problem though. – ryanyuyu Apr 21 '15 at 13:45
  • Check http://stackoverflow.com/questions/13743058/how-to-access-the-angular-scope-variable-in-browsers-console – Mario Levrero Apr 21 '15 at 13:46
  • Is the html `ng-model="PayDate"` or `ng-model="model.PayDate"`? – Ben Felda Apr 21 '15 at 13:46
  • @BenFelda like docummention points out, the field is bound as `data-ng-model="model.PayDate"` - Is far as I'm concerned, `PayDate` would indicate to `$scope.PayDate`. –  Apr 21 '15 at 13:48
  • 1
    @MarioLevrero it looks like that control creates own child scope. –  Apr 21 '15 at 13:55
  • @pwas Yep, just making sure. Just because your javascript shows what's on `$scope` doesn't mean you bound it correctly in your html. – Ben Felda Apr 21 '15 at 13:57

3 Answers3

0

Maybe try to inject $timeout in your controller. So timeout does a digest cycle first, and then post your data..

$scope.save = function() {
  $timeout(function(){
    $http.post(url, $scope.model)
       .success(function (data) {
            // some handle
        }).error(function () {
             // some handle
        });
    });
};

Does this solve your problem?

brammekuhh
  • 133
  • 1
  • 9
0

I think the problem is that your directive is creating a child scope with its own variable, that does not modify the parent's one. You can check this answer with the same situation.

** Update after the JS String update **

From documentation

Working with ng-model

The angular-bootstrap-datetimepicker directive requires ng-model and the picked date/time is automatically synchronized with the model value.

This directive also plays nicely with validation directives such as ng-required.

The angular-bootstrap-datetimepicker directive stores and expects the model value to be a standard javascript Date object.

Try to use a DateObject instead of a String

So, instead of

$scope.model.PayDate === "2015-04-29T22:00:00.000Z";

Use

$scope.model.PayDate === new Date("2015-04-29T22:00:00.000Z");
Community
  • 1
  • 1
Mario Levrero
  • 3,345
  • 4
  • 26
  • 57
  • Maybe but I 've only one controller in my html. So it looks like that the plugin for date time picker has a bug inside. –  Apr 21 '15 at 14:05
  • BTW. Converting Date to string fixes the problem (see my edit). So maybe scopes are build properly? –  Apr 21 '15 at 14:07
  • But Doc says is waiting for a Date Object. I've update my answer – Mario Levrero Apr 21 '15 at 14:14
  • I know, i use Date from JS. That snippet shows only the value that is got from server. Then it is coverted to JS Date. As you can see in my quesion, The value before post in JS Date. –  Apr 21 '15 at 14:22
0

I think you are having an issue with the timezone you're in (poland UTC+2 ?)

You are dealing with a Javascript Date which is in your current TimeZone GMT+2 So you'll see Thu Apr 30 2015 00:00:00 GMT+0200 but once it sends it back to the server its sent as an ISOString

new Date("2015-04-29T22:00:00.000Z")

output: Thu Apr 30 2015 00:00:00 GMT+0200 (YOUR TIMEZONE)

new Date("2015-04-29T22:00:00.000Z").toISOString()

output: "2015-04-29T22:00:00.000Z"

So those 2 dates are actually the same

dco
  • 317
  • 1
  • 13