Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

angular中如何在input元素上使用filter? #1

Open
natee opened this issue Jul 16, 2014 · 0 comments
Open

angular中如何在input元素上使用filter? #1

natee opened this issue Jul 16, 2014 · 0 comments
Assignees

Comments

@natee
Copy link
Owner

natee commented Jul 16, 2014

当把一个变量如日期显示在ng-model上时,有时需要对显示的内容进行格式化,但是ng-model上是不支持| filter这样的,因此需要单独写directive来实现。

<input type="text" ng-model="start_date" date-format disabled="disabled">
angular.module('dateRange',[]).directive('dateFormat', ['$filter',function($filter) {  
    var dateFilter = $filter('date');  
    return {  
        require: 'ngModel',  
        link: function(scope, elm, attrs, ctrl) {  

            function formatter(value) {  
                return dateFilter(value, 'yyyy-MM-dd'); //format  
            }  

            function parser() {  
                return ctrl.$modelValue;  
            }  

            ctrl.$formatters.push(formatter);  
            ctrl.$parsers.unshift(parser);  

        }  
    };  
}]);  
@natee natee self-assigned this Jul 16, 2014
@natee natee changed the title How to use filter in input element? Angular中如何在input元素上使用filter? Jul 16, 2014
@natee natee changed the title Angular中如何在input元素上使用filter? angular中如何在input元素上使用filter? Jul 16, 2014
@natee natee added bug and removed enhancement labels Jul 16, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant