简体   繁体   中英

AngularJS: Factory and filters - factory is not working

I have this code, and it 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 am trying to be more object oriented and modular, so I have made the following code, but it is 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 someone explain me the reason what is wrong with the second version of the code? Thanks!

You have two return statements in your factory. you need to create an object, assign method to it, then 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;
});

I have finally found the solution! There wasn't any error in the console, because everything was structured ok. But the problem was that my filters weren't returning anything. So I have simply added a return, and now everything is ok. Really a banal mistake. Here is the code:

var app = angular.module('twitterApp', ['twitterApp.services', 'ngSanitize']);

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;
});

app.filter('clearImage', function(StratoFactory) { 
return function(text) {
**return** StratoFactory.removeNormalStringFromImage(text);
};
});
app.filter('links', function(StratoFactory) { 
return function(text) {
**return**  StratoFactory.userName2Link(text);
};
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM