AngularJS中如何使用$http對(duì)MongoLab數(shù)據(jù)表進(jìn)行增刪改查
主頁面:
<button ng-click="loadCourse()">Load Course</button> <button ng-click="toggleAddCourse(true)">Add New Course</button> <ng-includce src="'course_list.html'"></ng-include> <ng-include src="'add_course.html'" ng-show="toggleAddCourseView"></ng-include> <ng-include src="'edit_course.html'" ng-show="toggleEditCourseView"></ng-include>
以上,頁面上顯示course_list.html,add_course.html和edit_course.html的內(nèi)容顯示與toggleAddCourseView和toggleEditCourseView值有關(guān),而toggleAddCourseView和toggleEditCourseView值將通過方法來控制。
在Mongolab上創(chuàng)建數(shù)據(jù)庫和表
→ https://mongolab.com
→ 注冊
→ 登錄
→ Create new
→ 選擇Single-node
勾選Sandbox,輸入Database name的名稱為myacademy。
→ 點(diǎn)擊新創(chuàng)建的Database
→ 點(diǎn)擊Add collection
名稱為course
→ 點(diǎn)擊course這個(gè)collection。
→ 多次點(diǎn)擊add document,添加多條數(shù)據(jù)
控制器
$scope.courses = [];
var url = "https://api.mongolab.com/api/1/databases/my-academy/collections/course?apiKey=myAPIKey";
var config = {params: {apiKey: "..."}};
$scope.toggleAddCourseNew = false;
$scope.toggleEditCourseView = false;
//列表
$scope.loadCourses = function(){
$http.get(url, config)
.success(function(data){
$scope.courses = data;
});
}
//添加
$scope.addCourse = function(course){
$http.post(url, course, config)
.success(function(data){
$scope.loadCourses();
})
}
//顯示修改
$scope.editCourse = function(course){
$scope.toggleEditCourseView = true;
$scope.courseToEdit = angular.copy(course);
}
//修改
$scope.updateCourse = function(courseToEdit){
var id = courseToEdit._id.$oid;
$http.put(url + "/" + id, courseToEdit, config)
.success(fucntion(data){
$scope.loadCourses();
})
}
//刪除
$scope.delteCourse = function(course){
var id = course._id.$oid;
$http.delete(url+ "/" + id, config)
.success(function(data){
$scope.loadCourses();
})
}
$scope.toggleAddCourse = function(flag){
$scope.toggleAddCourseView = flag;
}
$scope.toggleEditCourse = fucntion(flag){
$scope.toggleEditCourseView = flag;
}
course_list.html 列表
<tr ng-repeat="course in courses">
<td>{{$index+1}}</td>
<td>{{course.name}}</td>
<td>{{course.category}}</td>
<td>{{course.timeline}}</td>
<td>{{course.price | currency}}</td>
<td><button ng-click="editCourse(course)">Edit</button></td>
<td><button ng-click="deleteCourse(course)">Delete</button></td>
</tr>
add_course.html 添加
<form> <input type="text" ng-model = "course.name" /> <select ng-model="course.category"> <option>-Select-</option> <option value="development">Development</option> <option value="business">Business</option> </select> <input type="number" ng-model="course.timeline" /> <input type="number" ng-model="course.price"/> <button ng-click="addCourse(course)">Add</button> <button ng-click="toggleAddCourse(false)">Cancel</button> </form>
edit_course.html 更新
<form> <input type="text" ng-model="courseToEdit.name" /> <select ng-model ="courseToEdit.category"> <option>-select-</option> <option value="development">Development</option> <option value="business">Business</option> </select> <input type="number" ng-model="courseToEdit.timeline"/> <input type="number" ng-model="courseToEdit.price"/> <button ng-click="updateCourse(courseToEdit)">Update</button> <button ng-click="toggleEditCourse(false)">Cancel</button> </form>
以上所述是小編給大家分享的AngularJS中如何使用$http對(duì)MongoLab數(shù)據(jù)表進(jìn)行增刪改查的相關(guān)知識(shí),希望對(duì)大家有所幫助。
- JS基于設(shè)計(jì)模式中的單例模式(Singleton)實(shí)現(xiàn)封裝對(duì)數(shù)據(jù)增刪改查功能
- php數(shù)據(jù)庫的增刪改查 php與javascript之間的交互
- Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼(二)
- Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼(一)
- jsp+servlet+jdbc實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的增刪改查
- nodejs連接mongodb數(shù)據(jù)庫實(shí)現(xiàn)增刪改查
- Node.js操作mysql數(shù)據(jù)庫增刪改查
- js 如何實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的增刪改查
- js實(shí)現(xiàn)提交前對(duì)列表數(shù)據(jù)的增刪改查
相關(guān)文章
Angular?Ngrx?Store應(yīng)用程序狀態(tài)典型示例詳解
這篇文章主要為大家介紹了Angular?Ngrx?Store應(yīng)用程序狀態(tài)典型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Angularjs驗(yàn)證用戶輸入的字符串是否為日期時(shí)間
這篇文章主要介紹了Angularjs驗(yàn)證用戶輸入的字符串是否為日期時(shí)間,需要的朋友可以參考下2017-06-06
AngularJs 最新驗(yàn)證手機(jī)號(hào)碼的實(shí)例,成功測試通過
下面小編就為大家分享一篇AngularJs 最新驗(yàn)證手機(jī)號(hào)碼的實(shí)例,成功測試通過,具有很好的參考價(jià)值。希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11
Angular中ng-options下拉數(shù)據(jù)默認(rèn)值的設(shè)定方法
本篇文章主要介紹了Angular中ng-options下拉數(shù)據(jù)默認(rèn)值的設(shè)定方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Angular實(shí)現(xiàn)下拉框模糊查詢功能示例
這篇文章主要介紹了Angular實(shí)現(xiàn)下拉框模糊查詢功能,涉及AngularJS事件響應(yīng)及字符串查詢等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Facade Service暴露commands簡化代碼邏輯提高可訪問性組合性
在 Angular 應(yīng)用開發(fā)中,使用 Facade Service 暴露 commands(命令)以及訂閱這些 commands 是一個(gè)常見的設(shè)計(jì)模式,本文將詳細(xì)介紹在 Facade Service 中如何實(shí)現(xiàn)這一目標(biāo),并深入探討相關(guān)細(xì)節(jié),以及通過實(shí)際示例進(jìn)行說明2023-10-10
理解AngularJs篇:30分鐘快速掌握AngularJs
這篇文章主要介紹了理解AngularJs篇:30分鐘快速掌握AngularJs,詳細(xì)介紹了AngularJs所涉及的知識(shí)點(diǎn),有興趣的可以了解一下。2016-12-12
Angular8升級(jí)至Angular13遇到的問題解決
這幾天升級(jí)公司的一個(gè)Angular項(xiàng)目遇到了一些問題,下面這篇文章主要給大家介紹了關(guān)于Angular8升級(jí)至Angular13遇到的問題解決,文中介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01

