Angularjs filter error: “Error: Unknown provider: textProvider”

2,415 views
Skip to first unread message

Rahul J

unread,
Feb 25, 2013, 2:38:24 AM2/25/13
to ang...@googlegroups.com

I created a custom filter for my angularjs project similar to the following fiddle http://jsfiddle.net/tUyyx/.

myapp.filter('truncate',function(text,length){
    var end = "..."
    text = text.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    if (isNaN(length))
     length = 23;



    if (text.length <= length || text.length - end.length <= length) {
        return text;
    }
    else {
        return String(text).substring(0, length-end.length) + end;
    }

});

but when I use the filter i get the following error

Error: Unknown provider: textProvider <- text <- truncateFilter
    at Error (<anonymous>)
    at http://localhost/javascripts/lib/angular.min.js:28:236
    at Object.c [as get] (http://localhost/javascripts/lib/angular.min.js:26:13)
    at http://localhost/javascripts/lib/angular.min.js:28:317
    at c (http://localhost/javascripts/lib/angular.min.js:26:13)
    at Object.d [as invoke] (http://localhost/javascripts/lib/angular.min.js:26:147)
    at http://localhost/javascripts/lib/angular.min.js:28:335
    at Object.c [as get] (http://localhost/javascripts/lib/angular.min.js:26:13)
    at http://localhost/javascripts/lib/angular.min.js:99:481
    at o (http://localhost/javascripts/lib/angular.min.js:66:471) 

I have created my module like this. var myapp = angular.module('myapp', ['ngResource']);

What am i doing wrong?

Gaurav Sharma

unread,
Feb 25, 2013, 3:10:53 AM2/25/13
to ang...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Thanks and Regards,
Gaurav Sharma

Pascal Precht

unread,
Feb 25, 2013, 3:21:11 AM2/25/13
to ang...@googlegroups.com
Exactly. You have to write a function, which returns a function. FuncCeption :)

Gabriele Destefanis

unread,
Jun 13, 2013, 11:39:34 AM6/13/13
to ang...@googlegroups.com
Same problem using this handful seed: https://github.com/elsom25/angular-requirejs-html5boilerplate-seed 

I defined my custom filter in filters/StartFromFilter.js as:
define(['Console'], function (Console) {
"use strict";
Console.group("Entering StartFromFilter module.");
var filter = function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    }
   Console.groupEnd();
   return filter;
});

Then in filters/filters.js:
define([
  // Standard Libs
  'Console'       // lib/console/console
  , 'Underscore'  // lib/underscore/underscore
  // Application Filters
  , 'filters/StartFromFilter'
], function (Console, _, startFrom){
  "use strict";
  Console.group("Entering Filters module.");
  var filters = {
    startFrom: startFrom
  };
  Console.info("Registered filters: ", filters);
  var initialize = function (angModule) {
    Console.group("Entering Filters initialize.");
     Console.debug("Filters :",filters);
    _.each(filters,function(filter,name){
      Console.debug("Custom filters "+name+" initialized.");
      angModule.filter(name,filter);
    })
    Console.debug("Custom filters initialized.");
    Console.groupEnd();
  }
  Console.groupEnd();
  return {
    initialize: initialize
  };
});

But I get the error: 
Error: Unknown provider: inputProvider <- input <- startFromFilter

Can you help me?

Gabriele Destefanis

unread,
Jun 14, 2013, 5:48:40 AM6/14/13
to ang...@googlegroups.com
I solved my issue changing filters/StartFromFilter.js as
define(['Console'], function (Console) {
"use strict";
Console.group("Entering StartFromFilter module.");
var filter = [function() {
return function(input, start) {

       start = +start; //parse to int
       return input.slice(start);
   }
}];
Console.groupEnd();
return filter;
});


If you need this seed I've forked it including my filter example on https://github.com/destegabry/angular-requirejs-html5boilerplate-seed
Reply all
Reply to author
Forward
0 new messages