Nilt
Nilt

Reputation: 11

AngularJS - Struggling to pass value to factory

First i want to say that i'm fairly new to AngularJS, so it might be that i'm asking my question with some oddnes.

I'm trying to pass a string to a factory which in return gives me a result from my database. When i hardcode the values, everything works. But when i try to pass inn values from my view/controller things stop working.

Here is my factory:

healthServices.factory('Entry',
    function ($resource) {
        return $resource('http://localhost:60673/api/hierarchy/:id', { id: '@id'}, {
            query: { method: 'GET', isArray: true  }
        });
    });

Here is the controller im using:

$scope.changeData = function (type) {
    $scope.entry = new Entry();
    $scope.id = type;
    $scope.healthData = $scope.entry.$query({ id: $scope.id });
}

And this is how it looks in my html:

<button ng-click="changeData('someValue')">

At the moment i keep getting

TypeError: value.push is not a function

As i mentioned im quite new to this, so I might be far off. Any help would be very much appreciated.

Upvotes: 0

Views: 68

Answers (2)

Pratap A.K
Pratap A.K

Reputation: 4517

I am not seeing any wrong with your $resource configuration.

var myApp = angular.module('myApp',['ngResource']);

myApp.factory('Entry', function ($resource) {
            return $resource('http://localhost:60673/api/hierarchy/:id', { id: '@id'}, {
                query: { method: 'GET', isArray: true  }
            });
        }); 


myApp.controller('myCtrl', ['$scope', 'Entry', function($scope, Entry){

    $scope.changeData = function (type) {
        $scope.entry = new Entry();
        $scope.id = type;
        $scope.healthData = $scope.entry.$query({ id: $scope.id }); 

    }
}]);

i am getting below in console

GET http://localhost:60673/api/hierarchy/someValue

error lies on other part of the code, please post your controller completely.

Upvotes: 0

brewsky
brewsky

Reputation: 617

What is intended by this line of code?

     $scope.entry = new Entry();

Entry is your service you want to call.
You should pass this into your controller via dependency injection. Angular does the 'new' for you.

    myApp.controller('myCntrl', HomeCtrl);
    HomeCtrl.$inject = ['$scope', 'Entry'];

    function HomeCtrl($scope, Entry) {
        ...
    }

Upvotes: 1

Related Questions