Last Updated: February 25, 2016
· quocvu

Adding a AngularJS Filter to a MEAN JS app

This post shows how to add a 3rd party AngularJS filter and use it in your views. Let's use the angular-truncate filter as an example. It can be found at https://github.com/sparkalow/angular-truncate.

First, import the filter into the app

$ bower i --save angular-truncate

Second, to tell MEAN.JS to load this filter in your template, edit config/env/all.js and add a new element to the assets.lib.js array with public/lib/angular-truncate/src/truncate.js

'use strict';

module.exports = {
    app: {
        title: 'TestApp',
        description: 'Full-Stack JavaScript with MongoDB, Express, AngularJS, and Node.js',
        keywords: 'MongoDB, Express, AngularJS, Node.js'
    port: process.env.PORT || 3000,
    templateEngine: 'swig',
    sessionSecret: 'MEAN',
    sessionCollection: 'sessions',
    assets: {
        lib: {
            css: [
            js: [
        css: [
        js: [
        tests: [

Third, to inject it into your app, edit the public/config.js file and add a new element to the end of the array applicationModuleVendorDependencies with the module name truncate

'use strict';

// Init the application configuration module for AngularJS application
var ApplicationConfiguration = (function() {
    // Init module configuration options
    var applicationModuleName = 'test';
    var applicationModuleVendorDependencies = ['ngResource', 'ngCookies',  'ngAnimate',  'ngTouch',  'ngSanitize',  'ui.router', 'ui.bootstrap', 'ui.utils', 'truncate'];

    // Add a new vertical module
    var registerModule = function(moduleName, dependencies) {
        // Create angular module
        angular.module(moduleName, dependencies || []);

        // Add the module to the AngularJS configuration file

    return {
        applicationModuleName: applicationModuleName,
        applicationModuleVendorDependencies: applicationModuleVendorDependencies,
        registerModule: registerModule

Lastly, you may now use the filter in your view to truncate the description to 200 characters long as

{{ description | characters:200 }}

2 Responses
Add your response


thanks for the pro tip!
Since I just got started with MeanJS, your tip provides a nice overview of using 3rd party libs.
Would it be also possible to accomplish the same result using yeoman?


over 1 year ago ·

Hello Taras,
I do not believe yeoman can be used to alter existing code. It only generates code. To make use of it, you must generate the code the first time with this filter included. We don't always know all filters/modules we need upfront.

over 1 year ago ·