0

I have the scenario as follow:

I have a text box and button and whenever I add sth in textbox I want to add the text in the table my code is as follow:

 var app = angular.module('app', []);
app.factory('Service', function() {
    var typesHash = [ {
        id :1,
        name : 'lemon',
        price : 100,
        unit : 2.5
    }, {
        id : 2,
        name : 'meat',
        price : 200,
        unit : 3.3
    } ];

    var localId = 3;

    var service = {
        addTable : addTable,
        getData : getData,


    };
    return service;
    function addTable(name) {
        typesHash.push({id:localId++, name:name, price:100,unit:1});
    }
    function getData() {
        return typesHash;
    }
});
app.controller('table', function(Service) {
    //get the return data from getData funtion in factory
    this.typesHash = Service.getData();
    //get the addtable function from factory 
    this.addTable = Service.addTable;
});

and the plnkr is as follow:

plnkr

Now as you can see I add whatever inside the text in the table and everything works fine but now I want to add whatever inside the textbox and also I want to get some information from the servlet and add those to the table as well. so for that I use ajax call as follow:

function addTable(name) {
        typesHash.push({id:localId++, name:name, price:100,unit:1});
        var responsePromise = $http.get("http://localhost:8080/purchase/AddInfo");
             responsePromise.success(function(data, status, headers, config) {
                    typesHash.push( {id:data.id,name : data.name, price : data.price,unit:2.5 });
                });
    }

but when I use that I get he following error:

ReferenceError: $http is not defined

can anyone help? (just a quick note: this code is smaller version of my real code and I purposely used factory since I need it)

1
  • 3
    You would need to inject $http inorder to use it. app.factory('Service', function($http) { or app.factory('Service', ['$http', function($http) { .Marking for closure for typo Commented Feb 7, 2015 at 20:43

1 Answer 1

2

inside of your controller attr your should insert an $http argument:

app.controller('CTRL1', function($scope, $http){
    //Now your can use $http methods
})

or insert $http argument in your service decleration if you are using $http request methods from inside of your service

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.