5

i want to hide some part of the input text value using some filter.

app.directive('limtToDate', ['$filter', function ($filter) {
var dateFormat = "DD/MM/YYYY HH:mm"
return {
    restrict: 'A',
    link: function (scope, ielem, attrs) {
        scope.$watch(attrs.ngModel, function (v) {
            console.log('value changed, new value is: ' + v);
            $filter('limitTo')(ielem.val(), -5);
        });
    }
}}]);

http://jsfiddle.net/PqAhk/2/

well, my input text should just show 12:12 instead of 01/01/1970 12:12. and after editing the time, for example, if user change the time to 12:40 my ng-model has to be like following 1970/01/01 12:40

2
  • I guess limitTo limits the array to a specific length. Can you elaborate what are you trying to do in this line. $filter('limitTo')(ielem.val(), -5); Commented Jun 3, 2014 at 11:41
  • what, i realy want is just hidding the first part of the date "1970/01/01" and show the rest of the text value. for example if i change the time to 12:40 my ng-model will be equal 1970/01/01 12:40 Commented Jun 3, 2014 at 11:52

3 Answers 3

1

First, thank you all, this solution was created by @guru and many thanks to him.

http://plnkr.co/edit/VhsleIWMq8A4rJcVQDaw?p=preview

the solution takes advantage from $formatter and $parser related to the angularjs pipeline rendering.

https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

ps : this solution is not compatible with angularjs.2-rc.x

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

Comments

0

If you don't want your model to change then don't use two way binding:

<div ng-app = "fiddle">
    <div ng-controller="MyCtrl">  
        <table class="table">
            <input type="text" ng-value="fromDate | limitTo:-5" />
        </table>
    </div>
</div>

Better yet why not make from date a real date object:

$scope.fromDate = new Date(); // current date for demonstration, set to real value

And use the date filter:

<input type="text" ng-value="fromDate | date:'HH:mm'" />

1 Comment

i have updating my question to be much more clear. what i really want is just using filter to hide some part of the text value. in my case it will be "1970/01/01" but my ng-model should remain "1970/01/01 12:12" and if user edit time to 12:40 this will update my ng-model to follow "1970/01/01 12:40"
0

The filter in the link function -> $filter('limitTo')(ielem.val(), -5); filters the array and returns a new array with the filtered values. It has to be assigned back to the scope variable for the changes to get reflected.

Something like below. $scope.filteredValue= $filter('limitTo')(ielem.val(), -5);

Thats intersting. Though formatting can be easy by using $formatter in the ngModelCtl syncing the input data change back to the model can be tricky. You can use the ngModelCtl.$parsers and ngModelCtl.$formatters to set up the format and view value.

Here is a working Solution: http://plnkr.co/edit/1KqLsIwGJjwUIbs0fwmQ?p=preview

10 Comments

it works by adding the following line ielem. val ($filter ('limitTo') (ielem. val (), -5)); but this will affect my ng-model to be equal to 12.14 and what I want is just I hide the letter 190/01/01. @Jacob Goulden what I have to do sir after your post improvement, should I edit my post?
Thats intersting. Though formatting can be easy by using $formatter in the ngModelCtl syncing the input data change back to the model can be tricky. You can use the ngModelCtl.$parsers and ngModelCtl.$formatters to set up the format and view value. Here is a working solution: plnkr.co/edit/1KqLsIwGJjwUIbs0fwmQ?p=preview
hi, it's wonderfull, but when i use it in my project, i got NAN:NAN. and the formatValue is Undefined, if you can explain to me how it work i will gratefull :)
$formatters - has a array of functions used to format value for display purpose $parsers - has a array of functions used to populate value to the actual model linked. Further Reading: docs.angularjs.org/api/ng/type/ngModel.NgModelController
If possible post your non working code in plunker. I will check if I can help with that
|

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.