javascript - AngularJS: Factory and filters - factory is not working -


i have code, , works:

var app = angular.module('twitterapp', ['twitterapp.services', 'ngsanitize']);  app.filter('clearimage', function () {      return function (text) {          var str = text.replace(/_normal./g, '.');          return str;      };  });  app.filter('links', function () {      return function (text) {          var str = text.replace(/@([^ ']+)/g, function(u, screen_name) {              var link = '<a target=blank href="http://twitter.com/intent/user?screen_name=' + screen_name + '">' + u + '</a>';              return link;                      });          str = str.replace(/#([^ ']+)/g, function (t, hash) {              var link = '<a target=blank href="https://twitter.com/hashtag/' + hash + '?src=hash">' + t + '</a> ';              return link;                                });          return str;      };  }); 

i trying more object oriented , modular, have made following code, not working:

var app = angular.module('twitterapp', ['twitterapp.services', 'ngsanitize']);    app.factory('stratofactory', function() {      var factory = {};      return {      removenormalstringfromimage : function(text) {                     var str = text.replace(/_normal./g, '.');              return str;             },      username2link : function(text) {                     var str = text.replace(/@([^ ']+)/g, function(u, screen_name) {              var link = '<a target=blank href="http://twitter.com/intent/user?screen_name=' + screen_name + '">' + u + '</a>';              return link;              });          str = str.replace(/#([^ ']+)/g, function (t, hash) {              var link = '<a target=blank href="https://twitter.com/hashtag/' + hash + '?src=hash">' + t + '</a> ';              return link;                              });          return str;      }      };      return factory;        });      app.filter('clearimage', function(stratofactory) {       return function(text) {      stratofactory.removenormalstringfromimage(text);      };  });  app.filter('links', function(stratofactory) {       return function(text) {      stratofactory.username2link(text);      };  });

can explain me reason wrong second version of code? thanks!

you have 2 return statements in factory. need create object, assign method it, return it.

try this:

app.factory('stratofactory', function() {     var factory = {};     factory.removenormalstringfromimage = function(text) {             var str = text.replace(/_normal./g, '.');             return str;     }     factory.username2link = function(text) {          var str = text.replace(/@([^ ']+)/g, function(u, screen_name) {             var link = '<a target=blank href="http://twitter.com/intent/user?screen_name=' + screen_name + '">' + u + '</a>';             return link;         });         str = str.replace(/#([^ ']+)/g, function (t, hash) {             var link = '<a target=blank href="https://twitter.com/hashtag/' + hash + '?src=hash">' + t + '</a> ';             return link;         });         return str;     }      return factory; }); 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -