Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$ is not defined - jquery angular mixing

Previously I developed all my projects with jQuery, but recently started with Angular. I created a Angular boilerplate with Yeoman generator and extend my code from it.

I translated my ajax form post jQuery code to the following Angular controller.

'use strict';


angular.module('MyApp')
.controller('ShareformCtrl', function ($scope, $http) {
// create a blank object to hold our form information
// $scope will allow this to pass between controller and view
$scope.formData = {};

// process the form
$scope.processForm = function() {
    $http({
        method : 'POST',
        url : 'php/share_handling.php',
        data : $.param($scope.formData), // pass in data as strings
        headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
    })
    .success(function(data) {
        console.log(data);

        if (!data.success) {
            // if not successful, bind errors to error variables
            $scope.errorName = data.errors.name;
            $scope.errorSuperhero = data.errors.superheroAlias;
        } else {
            // if successful, bind success message to message
            $scope.message = data.message;
        }
    });
};
});

But jshint will fail on $.param

In my index I have angular and jquery implemented as follows:

<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>

So Angular should have access to $, but it doesn't or something else is happening here.

like image 607
moasking Avatar asked May 14 '26 17:05

moasking


1 Answers

To tell JSHint not be worried about $ create .jshintrc in root project directory with

{
  "globals": {
     "$": false
  }
}

Docs

like image 53
Ruslan Ismagilov Avatar answered May 17 '26 18:05

Ruslan Ismagilov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!