最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AngularJS中如何使用$http對(duì)MongoLab數(shù)據(jù)表進(jìn)行增刪改查

 更新時(shí)間:2016年01月23日 21:00:44   作者:Darren Ji  
這篇文章主要介紹了AngularJS中如何使用$http對(duì)MongoLab數(shù)據(jù)表進(jìn)行增刪改查的相關(guān)資料,需要的朋友可以參考下

主頁(yè)面:

<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>

以上,頁(yè)面上顯示course_list.html,add_course.html和edit_course.html的內(nèi)容顯示與toggleAddCourseView和toggleEditCourseView值有關(guān),而toggleAddCourseView和toggleEditCourseView值將通過(guò)方法來(lái)控制。

在Mongolab上創(chuàng)建數(shù)據(jù)庫(kù)和表

→ https://mongolab.com
→ 注冊(cè)
→ 登錄
→ 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ì)大家有所幫助。

相關(guān)文章

最新評(píng)論

砀山县| 定远县| 民勤县| 上林县| 邢台市| 缙云县| 酉阳| 莫力| 辛集市| 正镶白旗| 鄂托克旗| 龙游县| 都江堰市| 禹州市| 蒲城县| 嵩明县| 深州市| 琼中| 镇巴县| 桐梓县| 长泰县| 平塘县| 双桥区| 镇巴县| 雅江县| 团风县| 杂多县| 南召县| 靖安县| 阿克苏市| 延川县| 丰台区| 大新县| 泉州市| 格尔木市| 石台县| 英吉沙县| 宁晋县| 禹州市| 余姚市| 南澳县|