Angular input box uses filter to format the date, angularfilter

Source: Internet
Author: User

Angular input box uses filter to format the date, angularfilter

Angular date selector is used recently. However, you need to output the selected date to the input box. If the selected date is displayed as a timestamp by default, it does not meet the requirements, the format must be changed to a specific format, but the ng-model on the input cannot directly use the filter. Therefore, you need to format the content displayed here.

To find a solution online, write direve VE. The specific instance code is as follows:

JS

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);        }    };}]);


HTML

<input type="text" ng-model="start_date" date-format disabled="disabled">

Display Effect. You can modify the format in the Code by formatting it.




Why is Angularjs so popular?

If other friends have good originals or translations, try to recommend them to bole online. In this article, let's gradually find out why angular is so popular: Angular. js is a MV * (Model-View-Whatever, whether it is MVC or MVVM, unified by model Drive View) JavaScript framework, it is the SPA (single-page-application) application Framework launched by Google, which adds a lot of magic changes to our web application development. I can spend the whole day telling you why you have to try angular. js in a new project, but I think it's still a bit difficult to say. Data Binding and scopes (scope) are the first problems that emerge first: does angular support data binding? Next let's take a look at angular. js Data Binding:
Edit in plucker Insert your name: Echo: {user. name} in this code snippet, before explaining the details, I still want to try it out: Note: Do not be too anxious to understand ng-app at the moment. As you can see, what I input in input will be displayed in the back echo. How does this work? In short, angular ng-model (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.