7

i'm a newbie in angularjs i'm trying to bind a date property to a input(text) but i don't know how to format the date.

my json object controler:

$scope.datasource = {"prop1":"string data", "myDateProp":"\/Date(1325376000000)\/"}

my view:

<input type="text" ng-model="datasource.myDateProp" />

as result, i get the string "/Date(1325376000000)/" on my textbox.

how can i format this date?

asked Oct 11, 2012 at 10:44

1 Answer 1

3

(削除) What you need to do is have a look at http://docs.angularjs.org/api/ng.filter:date which is a filter that is available in angular by default.

It seems that you are passing additional stuff with the date. See the scenario here. ( /Date(*)/ ) . Except for the stuff in * everything else is not necessary to parse the date and the default angular filter wont be able to parse it. Either strip these additional stuff from the model, or alternatively, you could write your own filter to strip them on input. (削除ここまで)

EDIT :

Have a look at http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController ( and the example that is defined there! ). If you intend on reusing this in multiple places, I suggest you to do it in the method that ngModelController describes. Create a new directive and implement $render and $setViewValue on the ngModel.

If you just want to do this in one place, then an alternate would be to define a new model for the input. Something like

$scope.dateModel = "";

and use it

<input type="text" ng-model="dateModel" ng-change="onDateChange()"/>

In your controller, you will have to do something like :

$scope.$watch("datasource.myDateProp",function(newValue){
 if(newValue){
 convert(newValue);
 }
});
function convert(val){
 //convert the value and assign it to $scope.dateModel;
}
$scope.onDateChange = function(){
// convert dateModel back to the original format and store in datasource.myDateProp.
}
answered Oct 11, 2012 at 10:57
Sign up to request clarification or add additional context in comments.

2 Comments

ng-model="datasource.myDateProp" defines a bidirectional binding, i dont wanna lose the binding. following that example o lose that!
@FlavioOliveira Have a look at the changed answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.