【问题标题】:How can i add more then one functions in a service or factory?如何在服务或工厂中添加多个功能?
【发布时间】:2015-02-04 07:44:24
【问题描述】:

我必须写一个里面有很多功能的服务。这我必须注入控制器。 但是,然后我编写了一个具有 3 个或更多功能的工厂,角度找到了第一个,所有其他的都未定义。 - 为什么?

mySystem.factory('testFactory', function($http) {
return {
checkDates: function() {
    myData.VDate = myData.VDate.format('dd.MM.yyyy');
}

return {
    checkrequiered: function() {
    var check = true;
    if (myData.locId.lenght === 0) {
        check=false;
    }
    return check;
}

return {
    updateData: function() {
       '...'
    }
});

怎么了?

【问题讨论】:

    标签: angularjs service factory


    【解决方案1】:

    问题在于您有三个 return 语句,这意味着除了第一个之外的所有语句都将被忽略。返回单个对象中的所有函数:

    return {
        checkDates: function() {
            myData.VDate = myData.VDate.format('dd.MM.yyyy');
        },
        checkRequired: function() {
            return (myData.locId.length !== 0);
        },
        updateData: function() {
           '...'
        }
    };
    

    【讨论】:

    • 非常感谢您的回答。
    • 下一个问题:如何在函数中添加 $scope 变量?
    • @MarkusZuberhauser 我很确定您不应该在服务中使用$scope。这有点违背使用服务的目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    相关资源
    最近更新 更多