Angularjs 未定义$-jquery角度混合
以前我用jQuery开发了我的所有项目,但最近开始使用Angular。 我用Yeoman生成器创建了一个有角度的样板,并从中扩展了我的代码 我将ajax表单post jQuery代码翻译成以下Angular控制器Angularjs 未定义$-jquery角度混合,angularjs,jshint,Angularjs,Jshint,以前我用jQuery开发了我的所有项目,但最近开始使用Angular。 我用Yeoman生成器创建了一个有角度的样板,并从中扩展了我的代码 我将ajax表单post jQuery代码翻译成以下Angular控制器 'use strict'; angular.module('MyApp') .controller('ShareformCtrl', function ($scope, $http) { // create a blank object to hold our form infor
'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;
}
});
};
});
但是jshint将在$上失败。param
在我的索引中,angular和jquery实现如下:
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
因此Angular应该有权访问
$
,但它没有,或者这里正在发生其他事情。在代码顶部添加下面的注释以绕过jshint检查
/* global $ */
告诉JSHint不要担心
$
在根项目目录中创建.jshintrc
{
"globals": {
"$": false
}
}
谁在抛出错误提示或浏览器?coode是否在浏览器中工作?我要避免混合使用jQuery和Angular.jshint,因为它会抛出这个错误。您应该创建一个服务作为工厂,以使用$http POST作为方法。然后在控制器中使用该方法。在你的成功中也不需要做if-else语句。将.error()用于错误代码。看看它有多大帮助。不需要$.param($scope.formData)。data:$scope.formData可以正常工作。如果要传入jQuery,请使用“value”向angular injector Ok注册jQuery,这有助于构建它。在我的控制台中,我看到它正在发布,但没有发送数据。您确定
$scope.formData
不是空对象吗?console.log($scope.formData);正在打印出{},所以在post:Syeah上它是空的,所以检查一下为什么关于您的表单它是空的:DDoh!忘记了ng型号,对不起,伙计们:P谢谢你们的帮助,总有一天我会喜欢的。