1.HTML代码
<body ng-app="Power" ng-controller="userCtrl" ng-init="initData()"> 用户名:<input type="text" ng-model="user.username" /><br/> 密码:<input type="text" ng-model="user.password" /> <input type="button" value="提交" ng-click="saveUser()" /> </body>
2.service代码
this.saveUser=function(user){ var deferred = $q.defer(); $http.get("userAction!doNotNeedSession_add.action",{ params:user//传整个表单数据 }).success(function (data) { // 如果连接成功,延时返回给调用者 deferred.resolve(data); }) .error(function () { deferred.reject('连接服务器出错!'); }) return deferred.promise; };
3.controller代码
$scope.user={username:'',password:''}; $scope.saveUser=function () { userService.saveUser($scope.user).then(function (data) { console.info(data); }); };原文链接:https://www.f2er.com/angularjs/149152.html