Convert string to date input date inside angularjs relay

I have a repeater with a table row where I have a date that can be edited. The problem is that I cannot set a model with a string instead of a date object, and it is not possible to apply a filter to enter a date. What could be the workaround here?

thank

<tr data-ng-repeat="item in collection">
    <td data-ng-bind="item.name"></td>
    <td data-ng-bind="item.sharedDate | date:'MMMM dd, yyyy'"></td>
    <td>
        <span data-ng-bind="item.note"></span>
        <textarea data-ng-model="item.note"></textarea>
    </td>
    <td>
        <span data-ng-bind="item.expiryDate | date:'MMMM dd, yyyy'"></span>
        <input type="date" class="form-control" id="expiryDate" data-ng-model="(item.expiryDate | date:'MM/dd/yyyy')">
    </td>

      

+3


source to share


1 answer


data-ng-model

is a 2 channel data binding and you cannot use an expression. It must reference the scope variable to ngModel

be able to read and write.

If the value item.expiryDate

is not in the correct format for ngModel

then you must preprocess this in the controller before using it in the template.

You will need to do something like this first in your controller or directive.



_.each($scope.collection,function(item) { item.expiryDate = $filter('date')('MM/dd/yyyy'));

      

Now in your template you can use ngModel

2-way data binding for this value.

<input type="date" class="form-control" id="expiryDate" data-ng-model="item.expiryDate">

      

+2


source







All Articles