Angular.JS內(nèi)置服務(wù)$http對(duì)數(shù)據(jù)庫(kù)的增刪改使用教程
本文主要介紹的是Angular.JS內(nèi)置服務(wù)$http對(duì)數(shù)據(jù)庫(kù)的增刪改操作的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)看看詳細(xì)的介紹:
一、使用$http查詢MySQL數(shù)據(jù)
angular.module('app',[])
.controller('MyCtrl',function ($scope,$http) {
$http.get('http://127.0.0.1:80/user/getUsers')
.success(function (resp) {
console.log(resp);
})
.error()
//jQuery
/*$.get('url',function (data) {
});*/
})
對(duì)應(yīng)的后臺(tái)Java代碼:
public void getUsers(){
List<User> users = User.dao.find("select * from t_user");
renderJson(Users);
}
二、$http實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪改
(1)$http帶參數(shù)發(fā)送請(qǐng)求
(2)對(duì)MySQL數(shù)據(jù)增刪改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularJS $http</title>
<link rel="stylesheet" href="css/foundation.min.css" rel="external nofollow" >
<style type="text/css">
html,body{font-size:14px;}
</style>
</head>
<body style="padding:10px;" ng-app="app">
<div ng-controller="MyCtrl">
<input type="text" ng-model="id">
<input type="text" ng-model="name">
<button class="button" onclick="addUser()">添加</button>
<button class="button" onclick="delUser()">刪除</button>
</div>
</body>
<script src="js/angular.min.js"></script>
<script src="app.js"></script>
</html>
angular.module('app', [])
.controller('MyCtrl', function ($scope, $http) {
$scope.id=" ";
$scope.name=" ";
$scope.addUser = function () {
$http.post('http://127.0.0.1:80/user/addUser',{id:$scope.id, name:$scope.name})
.success(function (resp) {
if(resp.success){
alert("添加成功");
}
})
}
$scope.delUser = function () {
$htp.post('http://127.0.0.1:80/user/delUser',{id:$scope.id})
.success(function () {
if(resp.success){
alert('刪除成功');
}
})
}
})
后臺(tái)Java代碼:
public void addUser(){
String id = getPara("id");
String name = getPara("name");
User user = new User();
boolean isok = false;
if(id != null && id.equals("")){
isok = user.set("id",id).set("name",name).update();
}else{
isok = user.set("name",name).save();
}
renderJson("seccess",isok);
}
public void delUser(){
String id = getPara("id");
boolean isok = User.dao.deleById(id);
renderJson("seccess",isok);
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用Angular.js能帶來(lái)一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- AngularJS實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)庫(kù)信息
- 三種AngularJS中獲取數(shù)據(jù)源的方式
- AngularJS實(shí)現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實(shí)例
- 基于AngularJS實(shí)現(xiàn)頁(yè)面滾動(dòng)到底自動(dòng)加載數(shù)據(jù)的功能
- Angularjs實(shí)現(xiàn)多個(gè)頁(yè)面共享數(shù)據(jù)的方式
- 詳解angularJs中自定義directive的數(shù)據(jù)交互
- Angular.js如何從PHP讀取后臺(tái)數(shù)據(jù)
- Angularjs 滾動(dòng)加載更多數(shù)據(jù)
- 深入學(xué)習(xí)AngularJS中數(shù)據(jù)的雙向綁定機(jī)制
- 在 Angular6 中使用 HTTP 請(qǐng)求服務(wù)端數(shù)據(jù)的步驟詳解
- Angular.JS讀取數(shù)據(jù)庫(kù)數(shù)據(jù)調(diào)用完整實(shí)例
相關(guān)文章
Angularjs實(shí)現(xiàn)多個(gè)頁(yè)面共享數(shù)據(jù)的方式
本文給大家介紹使用Angularjs實(shí)現(xiàn)多個(gè)頁(yè)面共享數(shù)據(jù)的方式,通過定義一個(gè)共享服務(wù)service來(lái)實(shí)現(xiàn)此功能,對(duì)angularjs共享數(shù)據(jù)相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)2016-03-03
AngularJS 文件上傳控件 ng-file-upload詳解
這篇文章主要介紹了AngularJS 文件上傳控件 ng-file-upload詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-01-01
AngularJS中控制器函數(shù)的定義與使用方法示例
這篇文章主要介紹了AngularJS中控制器函數(shù)的定義與使用方法,結(jié)合具體實(shí)例形式分析了AngularJS控制器函數(shù)的定義、綁定及相關(guān)使用技巧,需要的朋友可以參考下2017-10-10
AngularJS 限定$scope的范圍實(shí)例詳解
這篇文章主要介紹了AngularJS 限定$scope的范圍實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Angular中使用$watch監(jiān)聽object屬性值的變化(詳解)
下面小編就為大家?guī)?lái)一篇Angular中使用$watch監(jiān)聽object屬性值的變化(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-04-04
詳解AngularJS1.x學(xué)習(xí)directive 中‘& ’‘=’ ‘@’符號(hào)的區(qū)別使用
這篇文章主要介紹了詳解AngularJS1.x學(xué)習(xí)directive 中‘& ’‘=’ ‘@’符號(hào)的區(qū)別使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08

