0

可以有动态文件资源吗?

这是我的工厂

factory('fileResourcedc', function ($resource) {
    var FileResourcedc = $resource(
                'xml/file.json',{},
                {
                    get:{method:'GET', isArray:false}
                }
            );
    return FileResourcedc;
})  

我从这里调用它:

    var deferred = $q.defer();
    var successFn = function (result) {
        if (angular.equals(result, [])) {
            deferred.reject("Failed  because empty : " + result.message);
        }
        else {
            deferred.resolve(result);
        }
    };
    var failFn = function (result) {
       deferred.reject("Failed dataconfResponse");
    };
    fileResourcedc.get(successFn, failFn);
    return deferred.promise;

请注意,在我的工厂中,文件名是硬编码的:

'xml/file.json'

我需要的是创建一个文件名参数并将其传递给工厂服务。是否可以?

提前谢谢

4

1 回答 1

0

这是我的解决方案:

factory('fileResourcedc', function ($resource) {
    var FileResourcedc = $resource(
                'xml/:myFile',
                {},
                {
                    get:{method:'GET', params:{myFile:""}, isArray:false}
                }
            );
    FileResourcedc.prototype.getCatalogue = function (fileName, successCat, failCat) {
        return FileResourcedc.get({myFile:fileName}, successCat, failCat);

    };
    return new FileResourcedc;
})

称呼:

        var deferred = $q.defer();
        var successFn = function (result) {
            if (angular.equals(result, {})) {
                deferred.reject("No catalogue");
            }
            else {
                deferred.resolve(result);
            }
        };
        var failFn = function (result) {
           deferred.reject("Failed catalogue");
        };
        fileResourcedc.getCatalogue("catalogues.json",successFn, failFn);
        return deferred.promise;

谢谢!

于 2013-04-09T21:46:42.920 回答