如何添加一个角的服务,以便控制器可以使用它?

时间:2022-08-25 23:02:33

I have a basic angular setup. I want to add another factory service so that the mainCtrl can use it.

我有一个基本的角度设置。我想添加另一个工厂服务,以便主ctrl可以使用它。

Here is my controller.js

这是我controller.js

angular.module('Manalator.controllers', [])
    .controller('MainCtrl', ['$scope', function ($scope, manaFactory) {
        manaFactory.testString();
    }]);

Here is my services.js

这是我services.js

angular.module('Manalator.services', [])
    .factory('cordovaReady', [function () {
        return function (fn) {
            var queue = [],
                impl = function () {
                    queue.push([].slice.call(arguments));
                };

            document.addEventListener('deviceready', function () {
                queue.forEach(function (args) {
                    fn.apply(this, args);
                });
                impl = fn;
            }, false);

            return function () {
                return impl.apply(this, arguments);
            };
        };
    }])
    .factory('manaFactory', [function(){
        this.testString = function(){
          return 'This works';
        };
    }]);

Here is my routes.js

这是我routes.js

angular.module('Manalator', ['ngRoute', 'Manalator.services', 'Manalator.controllers'])
    .config(function ($routeProvider) {
        $routeProvider
            .when('/', {
                controller: 'MainCtrl',
                templateUrl: 'partials/pages/main.html'
            })
            .when('/devotion', {
                controller: 'MainCtrl',
                templateUrl: 'partials/pages/devotion.html'
            })
            .when('/results', {
                controller: 'MainCtrl',
                templateUrl: 'partials/pages/results.html'
            })
            .otherwise({redirectTo: '/'});
    });

I get the following error:

我得到以下错误:

TypeError: Cannot read property 'testy' of undefined
    at new <anonymous> (controllers.js:3)
    at Object.i [as invoke] (main.min.js:1)
    at $get.f.instance (main.min.js:2)
    at m (main.min.js:1)
    at s (main.min.js:1)
    at $get.e (main.min.js:1)
    at main.min.js:1
    at p.$get.p.$eval (main.min.js:3)
    at p.$get.p.$apply (main.min.js:3)
    at main.min.js:1

It is a cordova phonegap setup with basic routes. I am new to angular. I have looked over the internet and im having trouble setting up a basic service to hold all my data so i can access it from all my routes. Any help would be appreciated.

这是一个科尔多瓦电话设置与基本路线。我刚接触角。我已经浏览过互联网,我很难建立一个基本的服务来保存我所有的数据,所以我可以从我所有的路线*问它。如有任何帮助,我们将不胜感激。

1 个解决方案

#1


2  

You will need to identify your services as a dependency of the controller.

您需要将您的服务标识为控制器的依赖项。

The first step is to make sure you define the services before the controller.

第一步是确保在控制器之前定义服务。

then change the controller code so that it names the services as a dependency.

然后更改控制器代码,使其将服务命名为依赖项。

angular.module('Manalator.controllers', ['Manalator.services'])
.controller('MainCtrl', ['$scope', function ($scope, manaFactory) {
    manaFactory.testString();
}]);

Hope this helps!

希望这可以帮助!

#1


2  

You will need to identify your services as a dependency of the controller.

您需要将您的服务标识为控制器的依赖项。

The first step is to make sure you define the services before the controller.

第一步是确保在控制器之前定义服务。

then change the controller code so that it names the services as a dependency.

然后更改控制器代码,使其将服务命名为依赖项。

angular.module('Manalator.controllers', ['Manalator.services'])
.controller('MainCtrl', ['$scope', function ($scope, manaFactory) {
    manaFactory.testString();
}]);

Hope this helps!

希望这可以帮助!