How do I create a custom format in a string with Angularjs?

Asked

Viewed 4,689 times

3

Well I have a date 19102015, if I do so

{{step.data | date:'dd/MM/yyyy'}} 

i can format the date and get 19/10/2015.

But in my case, I need something customized. I have a cnpj that comes like this: 00000000000000 and I need to format it like this

00.000.000/0000-00

Can anyone tell me how I create specific formations with Angularjs ?

Thank you

  • You cannot format with Jquery?

  • Hi @Paulohdsousa all project is already in Angularjs, do in Jquery would be my last option. Thanks

  • You can apply to Mask ui-br-cnpj-mask input with angular-ui-inputs> https://github.com/assisrafael/angular-input-masks.

  • @Eduardosilva I have already applied a mask, <input ng-model="data.cnpj" type="tel" ui-Mask="99.999.999/9999-99" ng-click="inserts

1 answer

1


What you’re looking for in Angularjs is called filter.

A filter takes an input value and optional configuration parameters, and returns a value that will be effectively displayed in the interface.

A filter is established as follows:

angular.module('MeuModulo', []).filter('nomeDoFiltro', function() {
    return function(input, arg1, arg2) {
        var out;
        // manipula input e seus argumentos...
        return out;
    };
});

For example, a filter that transforms a string into uppercase when parameter is true, and in lowercase when the parameter is false:

angular.module('MeuModulo', []).filter('case', function() {
    return function(input, uppercase) {
        var out = "";
        if ( uppercase ) {
            out = input.toUpperCase();
        } else {
            out = input.toLowerCase();
        }
        return out;
    };
});

This filter can then be used in your HTML:

<p>{{ 'minha string' | case:true }}</p>

Echoing after processed by Angularjs:

<p>MINHA STRING</p>

See more about creating filters in the Angularjs documentation:

Developer Guide: Filters

Creating the filter for the CNPJ is a workout!

  • my only doubt now is how to apply it within my scope. I managed to do an example, but could not integrate with my project var app = angular.module('myApp', []);&#xA;&#xA;app.filter('cpf', function(){&#xA; return function(cpf){&#xA; return cpf.substr(0, 3) + '.' + cpf.substr(3, 3) + '.' + cpf.substr(6, 3) + '-' + cpf.substr(9,2);&#xA; };&#xA;}); that’s the code that works.

  • But my project uses $Cope there when I apply not the right mainAppControllers.controller('MenuCtrl', ['myservice','$rootScope','$scope', '$routeParams', '$location',&#xA; function(myservice,$rootScope,$scope, $routeParams, $location){

  • http://jsfiddle.net/caumardegan/qgjfapyz/

  • @Claudiamardegan did not understand the problem... in the Fiddle you passed worked well, it was all right: in the column with the attribute ng-non-bindable the filter has not been applied, and on the other without the attribute, the filter has been applied... see print: https://www.dropbox.com/s/7ov99ybnetfqkq/snapshot1.png?dl=0

  • I upgraded your Fiddle with the filter being used in a service variable $scope. See help: http://jsfiddle.net/1yv9bnwL/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.