3

如果连接到我的 API 时出现错误,有人可以帮我弄清楚如何在我的 AngularJS 工厂中返回硬编码数据。我的硬编码数据位于另一个名为“dataFactory”的工厂中。感谢您的帮助。

service.factory("ScheduleFactory", ['$http', '$resource', '$q', 'dataFactory', function($http, $resource, $q, dataFactory) {
        var objFactory = {};

        objFactory.getDaysOfWeek = function(includeWeekends) {
            var days = [];
            var API  = $resource(restURL + '/daysOfWeek/');

            API.query()
                .$promise
                    .then(function(data) {
                        isEmpty = (data.length === 0);

                        if (!isEmpty) {
                            days    = data;
                        };
                    })
                    .catch(function(error) {
                        console.log("rejected " + JSON.stringify(error));

                        var data    = null;
                        days    = dataFactory.daysOfWeek;

                        console.log(days.length); // returns 5
                    });


            console.log("after promise " + days.length);  // returns 'after promise 0'

            return days;
        };

我的 dataFactory 定义如下:

dataApp.factory("dataFactory", function() {
    objDataFactory  = {};

    objDataFactory.daysOfWeek = [
        {   dayName:        'Monday'
            , dayAbbrev:    'Mon'
            , index:        1
            , isSelected:   false
            , isWeekday:    true    }
        , { dayName:        'Tuesday'
             , dayAbbrev:   'Tue'
             , index:       2
             , isSelected:  false
             , isWeekday:   true    }
        , { dayName:        'Wednesday'
             , dayAbbrev:   'Wed'
             , index:       3
             , isSelected:  false
             , isWeekday:   true    }
        , { dayName:        'Thursday'
             , dayAbbrev:   'Thu'
             , index:       4
             , isSelected:  false
             , isWeekday:   true    }
        , { dayName:        'Friday'
             , dayAbbrev:   'Fri'
             , index:       5
             , isSelected:  false
             , isWeekday:   true    }
    ];

    return objDataFactory;
});
4

2 回答 2

7

我想这就是你要找的东西:

service.factory("ScheduleFactory", ['$http', '$resource', '$q', 'dataFactory', function($http, $resource, $q, dataFactory) {
     var objFactory = {};

     objFactory.getDaysOfWeek = function(includeWeekends) {
          var API  = $resource(restURL + '/daysOfWeek/'), defObj = $q.defer();

          var daysQuery = API.query();
          daysQuery.$promise.then(function(data) {
                    //you can add anything else you want inside this function
                    defObj.resolve(data);
               }, function(error) {
                    //you can add anything else you want inside this function
                    defObj.resolve(dataFactory.daysOfWeek);
               });

            return defObj.promise;
        };

我已经跳过了任何数据格式和分配细节,因为无论如何你都有可能改变它们。通过将 API 查询包装在额外的 Promise 中,您可以获得操作返回内容的能力。如果您的 API 可以访问,您可以根据您的外部承诺从它返回数据。否则,您将根据您的外部承诺从其他服务返回您的硬编码数据。

于 2014-07-31T23:51:56.123 回答
2

没有 catch 块,使用常规的错误回调函数:

   API.query()
        .$promise
            .then(function(data) {
                    isEmpty = (data.length === 0);

                    if (!isEmpty) {
                        days    = data;
                 },
                 function(error) { // removed catch here
                    console.log("rejected " + JSON.stringify(error));

                    var data    = null;
                    days    = dataFactory.daysOfWeek;

                    console.log(days.length); // returns 5
                ...

请参阅文档

then(successCallback, errorCallback , notifyCallback) – 无论承诺何时被解决或被拒绝,一旦结果可用,就会异步调用成功或错误回调之一。使用单个参数调用回调:结果或拒绝原因。此外,在解决或拒绝承诺之前,可能会调用通知回调零次或多次以提供进度指示。

于 2014-08-01T00:06:23.057 回答