Ronald Peroutka
Ronald Peroutka

Reputation: 25

Ionic/Angular JS Controller doesnt recognize function

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???

Upvotes: 0

Views: 264

Answers (1)

lealceldeiro
lealceldeiro

Reputation: 14958

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); }])

Upvotes: 4

Related Questions