以前我用jQuery开发了所有项目,但最近开始使用Angular.我用Yeoman生成器创建了一个Angular样板,并从中扩展了我的代码.
我将jQuery代码的ajax表单翻译成以下Angular控制器.
'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;
}
});
};
});
Run Code Online (Sandbox Code Playgroud)
但是jshint会失败 $.param
在我的索引中,我实现了如下角度和jquery:
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
Run Code Online (Sandbox Code Playgroud)
所以Angular应该可以访问$,但它没有或其他事情发生在这里.
告诉JSHint不要担心$在根项目目录中创建.jshintrc
{
"globals": {
"$": false
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6995 次 |
| 最近记录: |