0

So, I'm new to angular and Ionic.

my Serives.js file looks like this:

angular.module('app.services', [])

.factory('serviceName', function () {
    var z = 2;

    return {
        doStuff: function (x) {
            return x * z;
        },
        doMoreStuff: function (y) {
            return y * z;
        }
    }
})

Now i want to use the function doStuff in one of my controller:

.controller('weineCtrl', ['$scope', '$stateParams',function ($scope, $stateParams, serviceName) {
var test = serviceName.doStuff(3); }])

Everytime I run the page i get an error:

TypeError: Cannot read property 'doStuff' of undefined
at new <anonymous> (controllers.js:21)
at Object.instantiate (ionic.bundle.js:18015)
at $controller (ionic.bundle.js:23417)
at Object.self.appendViewElement (ionic.bundle.js:59908)
at Object.render (ionic.bundle.js:57901)
at Object.init (ionic.bundle.js:57821)
at Object.self.render (ionic.bundle.js:59767)
at Object.self.register (ionic.bundle.js:59725)
at updateView (ionic.bundle.js:65400)
at ionic.bundle.js:65377

Whats wrong???

1 Answer 1

4

You need to include serviceName as another dependency in your controller like this:

.controller('weineCtrl', ['$scope', '$stateParams', 'serviceName', function ($scope, $stateParams, serviceName) {
var test = serviceName.doStuff(3); }])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.