『壹』 angularjs能夠調用restful api嗎
可以的
REST(表徵性狀態傳輸,Representational State Transfer)是Roy Fielding博士在2000年他的博士論文中提出來的一種軟體架構風格。RESTful風格的設計不僅具有更好的可讀性(Human
Readable),而且易於做緩存以及伺服器擴展(scalability)。REST風格體現在URL設計上:
每個URL對應一個資源
對資源的不同操作對應於HTTP的不同方法
資源表現形式(representation)通過Accept和Content-Type指定
AngularJS提供了$resourceService來更方便地與RESTful伺服器API進行交互,可以方便地定義一個REST資源,而不必手動所有的聲明CRUD方法。
參考文檔: https://docs.angularjs.org/api/ngResource/service/$resource
Resource Factory
$resourceService定義在ngResourceMole中,需要在你的HTML中引入這個Mole對應的JS,同時在你的APP中添加這樣一個依賴:
var app = angular.mole('helloApp, ['ngResource']);
然後為資源建立一個Factory:
app.factory('Notes', ['$resource', function($resource) {
return $resource('/notes/:id');
}]);
當然,你也可以不把$esource的實例放到Factory里,直接在控制器中存起來:var
Notes = $resource('/notes/:id)。
CRUD
在你的控制器中就可以對資源進行增刪改查了:
app.controller('NotesCtrl', ['$scope', 'Notes', function($scope, Notes) {
var notes = Notes.query(function(){
// GET: /notes
// Response: [{id: 1, content: 'hello'}, {id: 2, content: 'world'}];
var first = notes[0];
first.content = 'halo';
first.$save();
// POST: /notes/1 {id: 1, content: 'halo'}
// Response: {id: 1, content: 'halo'}
second.$delete();
// DELETE: /notes/2
});
var note = new Notes({content: 'xxx'});
note.$save();
// POST: /notes
// Response: {id: 3, content: 'xxx'}
}]);
PUT 操作
$resource提供了五種默認操作:get, query, save, remove, delete。你可以配置一個update操作來完成HTTP
PUT:
app.factory('Notes', ['$resource', function($resource) {
return $resource('/notes/:id', null, {
update: { method:'PUT' }
});
}]);
現在,你可以在控制器中獲取一個note並更新它:
var note = Notes.get({ id: 3}),
$id = note.id;
note.content = 'yyy';
Notes.update({ id:$id }, note);
// PUT /notes/3 {id: 3, content: 'yyy'}
現在你的Notes有六種操作了。這些操作有兩種調用方式:
通過資源類調用,例如:Notes.update({id:
xxx});
通過資源實例調用,例如:note.$update(),此時操作名需加前綴$。
具體的調用參數可參考文檔:
HTTP GET "class" actions: Resource.action([parameters], [success], [error])
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
non-GET instance actions: instance.$action([parameters], [success], [error])
其中,success參數為(value,
responseHeaders),error參數為(httpResponse)。
屬性/URL映射
上述例子中,我們看到note對象的id屬性會映射到URL中的:id(/notes/:id)。如果你的業務更加復雜,可以手動配置這個映射關系。例如:
var Notes = $resouce('/users/:userId/notes/:noteId', {
noteId: '@id',
userId: '@owner'
}
將會讀取note的owner和id屬性來生成URL,比如刪除note時:
// note === {id: 123, owner: 'alice', content: 'hello'}
note.$delete();
// DELETE: /users/alice/notes/123
在構造$resource時,多於的屬性映射會成為URL
Query。例如:
var Notes = $resouce('/notes/:id', {
id: '@id',
user: '@owner'
});
// note === {id: 123, owner: 'alice', content: 'hello'}
note.$delete();
// DELETE: /notes/123?user=alice
REST操作的聲明和調用中,多於的屬性會成為URL Query。例如:
var Notes = $resouce('/notes/:id', {id: '@id'}, {
update: {method: 'PUT', operator: 'bob'}
});
// note === {id: 123, content: 'hello'}
note.$update({trusted: true});
// PUT: /notes/123?operator=bob&trusted=true {id: 123, content: 'hello'}
響應轉換
有時基於既定的後台設計,無法提供完全RESTful的API,比如/notes返回的是一個分頁器對象,而非數組。此時,我們仍然可以使用$resource,但需要設置響應轉換回調。例如:
var Notes = $resouce('/notes/:id', null, {
pager: {
method: 'GET',
transformResponse: function(data, headers){
// Server respond:
// data = {currentPage: 1,
// totalPage: 20,
// pageSize: 2,
// content: [{id: 1, content: 'hello'}, {id: 2, content: 'world'}]}
var pager = JSON.parse(data);
return pager.content;
}
}
});
var notes = Notes.query(function(){
// GET: /notes
// notes === [{id: 1, content: 'hello'}, {id: 2, content: 'world'}]
});
類似響應重寫,你還可以設置請求轉換transformRequest。
雖然$resource的設計可以支持絕大多數的URL和內容表示設計,但如果你發現$resource的使用過程極其復雜,那可能是你的伺服器API並不滿足RESTful風格。
地址: http://harttle.com/2015/06/05/angular-resource.html
『貳』 前端基於angular有哪些碩士論文題目方向可以寫
原創
按照要求定製
包修改
包過
直到通過為止
誠信指導,
『叄』 如何評價大漠窮秋的文章《Vue從Angular裡面抄了哪些東西》
Can we forge
『肆』 如何評價大漠窮秋的文章《angular有哪些地方比vue更優秀
Angular2相對於Vue的優勢在Vue2.0之後已經削弱很多了。比如SSR(Vue2已經原生支持)和全棧式框架(官方推薦Vuex+Vue-Router),native端也有阿里巴巴的weex。
但是Angular2在API設計的完整度和覆蓋度上還是Vue所不能及的,主要有這么幾點
1. Angular2原生Form支持:
Angular2原生的Form模塊功能相當強大。除了雙向綁定之類的基本功能,還能通過programatic API 控制dom元素的表單行為。也有成型API提供自定義validator。這一點Vue只有v-model和第三方庫。對於後台之類的重表單應用,還是Ng2有優勢。
2. 依賴注入
無論喜不喜歡DI,這就是Angular2的強大功能之一。有DI可以在不改變代碼結構的情況下完成功能替換。(如,在desktop和mobile有不同功能,可以通過注入不同service實現,而共用相同的template和directive)。Vue則需要程序員自己規劃代碼組織,用來支持共享組件。DI也可以用於類似mole local state的功能。比如,一個視頻播放控制項有幾個子組件完成,子組件需要分享一個狀態。這一點Angular2有原生的service injection pattern。而Vue則沒有官方推薦。
3. 對標准向後兼容
Angular2在一些細節上對標准有更好的支持。比如 list differ 演算法中 Angular2 可以支持實現了Symbol.iterator的對象,而Vue只能支持Array。對Observable和Promise,Angular2在應用的各個地方,甚至模板級別都有支持(async pipe)。而Vue需要vue-rx等第三方庫支持。Angular2的組件有shadow dom的實現可以選擇,而Vue目前還沒有。