Use constant as filter parameter in AngularJS

I’ ve a code like this

{{ aDate | bxDate : 'WhitoutTimePart' }}

I want to change the string ‘WhitoutTimePart’ as a constant so that, if one day the value change, i don’t have to go in every html file to do the modification.

Something like that :

{{ aDate | bxDate : bxdate.whitoutTimePart }}

Using an enum or a constant would be the best …

Any ideas ?

Thanks

Answer

You can have a constants.js file in your project where you define this and other similar constants like this.

 (function(){
          
   app.constant('filterParam', 'WhitoutTimePart');
   
 }).call(this);

Then you can import the constant in your controllers and asign it to a scope variable

(function () {
    'use strict';

angular.module('myModule')
    .controller('MyController', ['$scope','filterParam', MyController]);


function MyController($scope, filterParam) {

    $scope.filterParam = filterParam;        

})();

Then finally in your html

{{ aDate | bxDate : filterParam }}

Hope this was helpful to you

Leave a Reply

Your email address will not be published. Required fields are marked *