'best way to format ngModel for input type date

I know input[date] require date type from JSON, then I format ngModel value with new Date(json.date) , it's works

2015-11-15 become Mon Nov 15 2015 02:00:00 GMT+0200 (CEST)

But I need the value be a string 2015-11-15 for my webservice.

What is the best way to be ensure it works with input but keep available for my webservice please?



Solution 1:[1]

In whatever controller that calls your web service, you can use the same date: filter used in the dom {{myDateObjectThatNeedsToBeFormatted | date: 'yyyy-MM-dd'}} in your controller.

Simply inject the $filter service!!!

//Controller
angular.controller("myControllerThatCallsAWebservice", ["$scope","$filter", "myWebService", myNamedControllerFn]);

function myNamedControllerFn($scope, $filter, myWebService) {
   $scope.myDateVariableThatIsBoundToNgModel = new Date(2013, 9, 22);

   $scope.isolatedFnForConvertingYourDateToDesiredString = function alwaysNameFunctionsForDebuggingFn() {
       return $filter('date')($scope.myDateVariableThatIsBoundToNgModel, "yyyy-MM-dd")
   }

   myWebService.webServiceCallToDoStuff(
       $scope.isolatedFnForConvertingYourDateToDesiredString()
   );  
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sean Larkin