Encountering issues with AngularJS ng-click and $http post functionality

ReactJS

theApp.controller('MenuSideController', ['$scope', 'CategoryService', 
  function ($scope, CategoryService){
    CategoryService.getList()
    .success(function(list){
        $scope.list = list;
    });
    $scope.menuType = function(id) {
        $http.post('post_es.php', {'cat': $scope.username, 'pswd': $scope.userpassword, 'email': $scope.useremail}).success(function(data, status, headers, config) {
            if (data.msg != '') {
                $scope.msgs.push(data.msg);
            } else {
                $scope.errors.push(data.error);
            }
        }).error(function(data, status) { // called asynchronously if an error occurs
            // or server returns response with an error status.
            $scope.errors.push(status);
        });
    }
  }
]);

CSS

<li ng-repeat="company in list"><a href="#" ng-click="menuType(company.id)">{{ company.name }}</a></li>

When trying to execute the menuType function above, it results in the following error:

ReferenceError: $http is not defined

Answer №1

In order to utilize the $http service in your controller, you must include it as a dependency.

theApp.controller('MenuSideController', [
  '$scope',
  'CategoryService',
  '$http',
  function ($scope, CategoryService, $http) {

By adding it as a dependency, the $http service becomes accessible for use within the controller. The same principle applies to filters, directives, and other services.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Default Selection Issue with Radio Buttons in AngularJS

I am currently encountering an issue with the code snippet included in my directive template '<li ng-repeat="f in foos">' + '<input type="radio" ng-change="foo(f.key)" ng-model="selectedFoo" name="foos" id="{{f.key}}" value="{{f.ke ...

The proper way to send a string array parameter to an MVC WebApi controller

Starting my journey with AngularJS, I'm faced with the challenge of passing a JSON object that includes an array of strings to an MVC WebApi GET method. However, no matter what I try, I cannot seem to get the WebAPI controller to receive the correct v ...

Dealing with ng-repeat and button alignment across Chrome, Edge, and Firefox

Can someone help me understand this strange behavior I am experiencing across all browsers? <div style="margin-top:5px"> <button translate="clear" ng-click="xyz.clear()" class="btn btn-default"></button> <butt ...

Remove dynamically created elements from an AngularJS div

Is there a way to remove an item from the criteria list when clicking the delete button? Currently, only the filter is being refreshed and not the tables. Any suggestions on how to resolve this issue? HTML <ul class="list-unstyled"> <l ...

The static files in Spring Boot's web main resources are not automatically reloading when changed outside of STS

I'm currently working on an AngularJS project using Spring Boot, with the HTML/JS/CSS stored in src/main/resources/static. The issue I am facing is that when I make changes to these files outside of Eclipse and then refresh the browser, the changes d ...

Issue with undefined arrays in the Angular merge sort visualization tool

I am currently working on developing a visualizer for sorting algorithms using Angular. However, I have encountered some difficulties while implementing merge sort. As a Java programmer, I suspect that there may be an issue with my TypeScript code and the ...

Basic image manipulation features integrated with AngularJS

Looking to incorporate some light image layering, scaling, and positioning within a specific box on an AngularJS enabled web page? What is the recommended approach or convention for achieving this functionality? Is using jQuery the best option for impleme ...

"Enhance the Angular UI datepicker by updating disabled dates and refreshing

Has anyone found a solution for updating disabled dates in an Angular UI datepicker based on a server response? https://github.com/angular-ui/bootstrap/issues/779 One potential solution is to create another directive that will require the datepicker&ap ...

Improving animation performance on mobile devices using AngularJS

I've reached the final stages of developing a mobile application using AngularJS wrapped in a cordova webview. However, I'm encountering some issues with the panel transition animations. After experiencing strange behavior with ngAnimate, I deci ...

What could be causing the 403 Error when using Blogger API with AngularJS?

I'm relatively new to working with 3rd Party APIs and I'm currently exploring how to integrate Blogger's API into my AngularJS website to create a blog feed feature. After setting everything up, I've made a request and received a 200 s ...

Deep-diff JavaScript functions are not permissible for use

In my AngularJS application, I am currently attempting to utilize a JavaScript package. To reference it in my index.html file, I added the following code: <script src="deep-diff-0.3.1.min.js"></script> Furthermore, in my controller, I am usin ...

Chrome showing random 0 status for $http and $ajax requests

Occasionally, an issue arises on the website where the browser starts returning status 0 for XMLHTTPRequest requests randomly. This problem applies to requests made through both the jquery ajax function and the $http resource in angularJS. Curiously, the ...

Setting a value to ng-model in AngularJS

I am having some trouble using ng-model with multiple dropdowns. My goal is to have the first select option set to empty, which should then make the rest of the dropdowns also show the empty option. <select ng-model="ddl"> <option></option ...

Retrieve specific components of objects using a GET request

When visitors land on my web app's homepage, a GET request is triggered to fetch a current list of Stadiums stored in the database through my API. However, the Stadium objects retrieved are packed with unnecessary data, particularly extensive arrays o ...

Click event to focus on AngularJS TinyMCE textarea

My current project utilizes the tinymce text editor in combination with the angularJS framework. I obtained the directive from here and successfully integrated the editor using the provided example on GitHub! Initially, everything was working smoothly wit ...

Discover ways to circumvent using multiple conditions in a switch statement for a solitary object

Receiving an object from the client side in this format: var condition={"bedrooms":"1,2,3,4","Inhibition":"1,6","possession":"3","id":"8",toilets:"1,2",...,} The object must have the same keys and only be a single object, but its length can vary (1/2 ...

Storing an array within an AngularJS service for better performance

As someone who is relatively new to AngularJS, I am still in the process of understanding how to utilize services for fetching data in my application. My aim here is to find a method to store the output of a $http.get() call that returns a JSON array. In ...

Reading an irregular JSON file with AngularJS if statement

I received a json data with the following structure: [ { "level" : "1", "name" : "TIER 1 - CORE FOUNDATIONS", "required" : "13", "completed" : "9", "planned" : "0", "subcategories" : [ { "level" : "2", ...

Incorporating Angular into the script code of the extension content

I am looking to customize text fields on a website using a chrome-extension. However, the website is built with Angular, so I need to modify the text fields using Angular code. To incorporate Angular in my extension's scope, I am attempting to declar ...

Is it possible to incorporate a combination of es5 and es2015 features in an AngularJS application?

In my workplace, we have a large AngularJS application written in ES5 that is scheduled for migration soon. Rather than jumping straight to a new JS framework like Angular 2+ or React, I am considering taking the first step by migrating the current app to ...