AngularJS:工厂和过滤器 - 工厂不工作

时间:2014-11-27 12:08:13

标签: javascript angularjs filter factory

我有这段代码,它有效:



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;
    };
}); 
&#13;
&#13;
&#13;

我正在尝试更加面向对象和模块化,所以我制作了以下代码,但它不起作用:

&#13;
&#13;
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);
    };
});
&#13;
&#13;
&#13;

有人可以解释一下第二版代码出错的原因吗?谢谢!

2 个答案:

答案 0 :(得分:0)

您的工厂有两个退货单。 你需要创建一个对象,为它分配方法,然后返回它。

试试这个:

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

答案 1 :(得分:0)

我终于找到了解决方案!控制台中没有任何错误,因为一切都结构良好。但问题是我的过滤器没有返回任何东西。所以我只是添加了一个回报,现在一切都好。真的是一个平庸的错误。这是代码:

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