简体   繁体   English

Angular提供程序必须从$ get工厂方法返回一个值

[英]Angular provider must return a value from $get factory method

So I have a problem with my angular factory that I've been trying to solve for a while now. 因此,我的角度工厂遇到了一个问题,我已经尝试解决了一段时间。 I guess my knowledge in angular isn't quite enough here. 我想我对角度的了解还不够。

Most of the code I will post here is reused code from another part of the solution that I didn't write. 我将在此处发布的大多数代码是我未编写的解决方案另一部分中的重用代码。 So that's probably the reason why I can't figure this out. 因此,这可能就是我无法弄清楚这一点的原因。 Also this is the first ever factory I've made. 这也是我建造的第一家工厂。

(function () {
'use strict';

angular.module('app.coordinate-transformer-service', [])
    .factory('coordinateTransformerService', coordinateTransformerService);

coordinateTransformerService.$inject = ['proj4js', 'gmaps'];

function coordinateTransformerService(proj4js, gmaps, objectsEpsgCode) {

    var factory = {};

    factory.$get = ['proj4js', function coordinateTransformerFactory(proj4js) {
        return new coordinateTransformerService(proj4js, gmaps, objectsEpsgCode);
    }];

    var mapProjection = 'EPSG:4326', // WGS84 latlong
            objectsProjection = 'EPSG:' + objectsEpsgCode,
            transformCoordinatesToLatLng = function (point) {
                if (objectsProjection === mapProjection) {
                    return new gmaps.LatLng(point.y, point.x);
                }

                var newPoint = {
                    x: point.x,
                    y: point.y
                };
                proj4js.transform(new proj4js.Proj(objectsProjection), new proj4js.Proj(mapProjection), newPoint);
                return new gmaps.LatLng(newPoint.y, newPoint.x);
            };

    factory.transformLatLngToProject = function (latlng) {
        if (!factory.isFunction(latlng.lng) || !factory.isFunction(latlng.lat)) {
            throw new Error("No functions lat() or lng() exists on parameter latlng");
        }

        var newPoint = {
            x: latlng.lng(),
            y: latlng.lat()
        };

        proj4js.transform(new proj4js.Proj(mapProjection), new proj4js.Proj(objectsProjection), newPoint);
        newPoint = roundPointsToCorrectDecimals(newPoint);

        return newPoint;
    };

    factory.roundPointsToCorrectDecimals = function (point) {
        var noDecimals = objectsEpsgCode === 4326 ? 5 : 0;

        if (!point || !point.x || !point.y) {
            throw new Error("Point, x or y is null or not defined");
        }

        return {
            x: point.x.toFixed(noDecimals),
            y: point.y.toFixed(noDecimals)
        };
    };

    factory.isFunction = function (functionToCheck) {
        var getType = {};
        return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
    };

    return factory;
};

//coordinateTransformerService.prototype = {
//    constructor: coordinateTransformerService
//};
//return coordinateTransformerService;
})();

I get the error "Provider 'proj4js' must return a value from $get factory method." 我收到错误消息“提供程序'proj4js'必须从$ get工厂方法返回一个值”。 And to be honest I don't know what the factory.$get function I have does. 老实说,我不知道我的factory。$ get函数做什么。 That is part of the code I didn't write. 那是我没有写的代码的一部分。 Proj4js is a library that convert coordinates from one system to another. Proj4js是一个将坐标从一个系统转换到另一个系统的库。 So we load objects in from the server to display them on a google map, and some of that don't use latitude longitude. 因此,我们从服务器加载对象以在Google地图上显示它们,其中一些不使用纬度经度。 So that is why we need to transform them with proj4js. 这就是为什么我们需要使用proj4js对其进行转换。 If that info would help anyone. 如果该信息可以帮助任何人。

But I know a factory method needs to return an object. 但是我知道工厂方法需要返回一个对象。 But am I not doing that already? 但是我还没有这样做吗? Maybe it is that factory.$get function that is the problem, as I've said I don't know the purpose of it... But removing it doesn't help^^ 也许是因为factory。$ get函数是问题所在,正如我说过的,我不知道它的目的是什么……但是删除它并没有帮助^^

Also, don't mind that it's called coodinateTransformerService and not coordinateTransformerFactory. 另外,不要介意它被称为coodinateTransformerService而不是ordinateTransformerFactory。 I used a service first, just didn't care to rename it until I got it to work. 我首先使用了一项服务,只是在我开始使用它之前并不关心对其进行重命名。

EDIT: This may help. 编辑:这可能会有所帮助。 In angular.js where the error is thrown: 在angular.js中引发错误的地方:

function enforceReturnValue(name, factory) {
return function enforcedReturnValue() {
  var result = instanceInjector.invoke(factory, this, undefined, name);
  if (isUndefined(result)) {
    throw $injectorMinErr('undef', "Provider '{0}' must return a value from $get factory method.", name);
  }
  return result;
};
}

result gets undefined. 结果变得不确定。 Name is proj4js and factory is this which a colleague made: 名称是proj4js,而同事制造的工厂是这个:

(function () {
'use strict';

angular.module('blocks.proj4js').factory('proj4js', proj4jsFactory);

proj4jsFactory.$inject = ['$window'];
function proj4jsFactory($window) {
    return $window.proj4js;
}
})();

As I see you mixed up the service and provider. 如我所见,您混淆了服务和提供者。 The easiest way to meet your needs will be: 满足您需求的最简单方法是:

angular.module('app.coordinate-transformer-service', [])
    .service('coordinateTransformerService', CoordinateTransformerService);

CoordinateTransformerService.$inject = ['proj4js', 'gmaps', 'objectsEpsgCode'];

function CoordinateTransformerService(proj4js, gmaps, objectsEpsgCode) {

    var mapProjection = 'EPSG:4326', // WGS84 latlong
            objectsProjection = 'EPSG:' + objectsEpsgCode,
            transformCoordinatesToLatLng = function (point) {
                if (objectsProjection === mapProjection) {
                    return new gmaps.LatLng(point.y, point.x);
                }

                var newPoint = {
                    x: point.x,
                    y: point.y
                };
                proj4js.transform(new proj4js.Proj(objectsProjection), new proj4js.Proj(mapProjection), newPoint);
                return new gmaps.LatLng(newPoint.y, newPoint.x);
            };

    this.transformLatLngToProject = function (latlng) {
        if (!this.isFunction(latlng.lng) || !this.isFunction(latlng.lat)) {
            throw new Error("No functions lat() or lng() exists on parameter latlng");
        }

        var newPoint = {
            x: latlng.lng(),
            y: latlng.lat()
        };

        proj4js.transform(new proj4js.Proj(mapProjection), new proj4js.Proj(objectsProjection), newPoint);
        newPoint = roundPointsToCorrectDecimals(newPoint);

        return newPoint;
    };

    this.roundPointsToCorrectDecimals = function (point) {
        var noDecimals = objectsEpsgCode === 4326 ? 5 : 0;

        if (!point || !point.x || !point.y) {
            throw new Error("Point, x or y is null or not defined");
        }

        return {
            x: point.x.toFixed(noDecimals),
            y: point.y.toFixed(noDecimals)
        };
    };

    this.isFunction = function (functionToCheck) {
        var getType = {};
        return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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