ngResource已经似乎很简单,以落实事情...
使用Restangular over ngResource有什么优点/缺点?
1.1.3 $resource将返回promises并且可以使用最新的PR提交来实现.是否会提供未来支持$resource以支持Restangular所做的其他动词?如果发生这种情况,Restangular似乎会消失并变得不可靠.
当我查看Angular2文档时,我找不到像Angular1的ngResource那样的任何REST辅助模块.我发现它在Angular1中非常有用,所以我想在Angular2中有类似的东西,但我唯一能找到的东西是'angular2/http'它不包含任何类似的东西(或者我的搜索不够好?).
虽然很明显我自己很容易实现类似的服务,Angular2团队是否有任何现成的模块,或者是否会有一个?
我正在编写一个简单的库应用程序,以便为使用AngularJS的大型项目做好准备.在网上阅读了很多关于使用$resource与RESTful API交互的内容之后,我认为它可能会提供一些节省时间和扩展的好处来实现它而不是$http用于每个请求.问题是,由于某种原因(我不是CORS的专家,并且请求是跨域发送的),在使用$save我的Node.js控制台显示的方法时:
OPTIONS /books 200 1ms - 161b
Run Code Online (Sandbox Code Playgroud)
使用该query()方法工作正常 - 节点控制台显示:
GET /books 200 1ms - 228b
Run Code Online (Sandbox Code Playgroud)
此时我已经被困了几个小时,尝试下面的变化,但它总是最终成为OPTIONS请求而不是POST(这应该是根据Angular文档应该是该$save方法).
app.js
var libraryApp = angular.module('libraryApp', ['ngResource', 'ngRoute', 'libraryControllers']);
libraryApp.factory('$book', ['$resource', function ($resource) {
return $resource('http://mywebserver\\:1337/books/:bookId', { bookId: '@bookId' });
}]);
Run Code Online (Sandbox Code Playgroud)
controllers.js
var libraryControllers = angular.module('libraryControllers', []);
libraryControllers.controller('BookCtrl', ['$scope', '$book', function($scope, $book) {
...
$scope.addBook = function () {
var b = new $book;
b.isbn = "TEST";
b.description = "TEST"; …Run Code Online (Sandbox Code Playgroud) 我有一个角度响应,期望一个数组和服务调用传递一个数组(可以在chrome dev工具的网络选项卡中看到它).
但我在chrome控制台中遇到以下错误.
资源配置出错.包含一个对象但得到一个数组的预期响应
这是我的角度服务: -
physicalServerModule.factory("physicalServerServices", ['$resource',
function ($resource) {
var host = app.general.host;
var port = app.general.port;
var serverItemPath = 'v1/physicalserver/:x';
var serverPath = 'v1/physicalserver/list';
return {
physicalServer: function () {
return $resource(host + serverPath,{}, {
query: {
method: 'GET',
isArray: true
},
create: {
method: 'POST'
}
});
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
我打电话给我的服务如下: -
var tileServiceCall = physicalServerServices.physicalServer();
tileServiceCall.get({},{}).$promise.then(function (response) {
app.meta.physicalserver.tileItems = JSON.stringify(response);
}, function (error) {
alert("error");
});
Run Code Online (Sandbox Code Playgroud)
我的angularjs版本是1.2.15可以有人指出我的根本原因吗?
现在,我知道在angularjs调用中为动态标题设置标记的唯一方法就是$http这样:
return $http.get({
url: 'https://my.backend.com/api/jokes',
params: {
'jokeId': '5',
},
headers: {
'Authorization': 'Bearer '+ $scope.myOAuthToken
}
});
Run Code Online (Sandbox Code Playgroud)
但我想弄清楚如何通过$ resource传递这个,这里有一些伪代码不起作用:
...
.factory('myFactory',
['$resource',
function($resource){
return {
jokes: $resource('https://my.backend.com/api/jokes', null, {
query: {
method: 'GET'
}
})
};
}
]
);
...
return myFactory.jokes.query({
'jokeId': '5',
'headers': {
'Authorization': 'Bearer '+ $scope.myOAuthToken
}
});
Run Code Online (Sandbox Code Playgroud)
如何将动态标题传递给angularjs的$ resource?
可以在创建时定义一个$resource始终具有相同默认值的Angular new()吗?
例如,如果我有以下资源定义:
var Drawer = $resource('/drawer/:drawerId', { drawerId: '@id'});
Run Code Online (Sandbox Code Playgroud)
Drawer对象需要有一个"socks"属性,我希望它始终被初始化为一个空数组[],也许还有一些其他像'timesOpened'到0,或类似的东西.
这样做的唯一方法是:
var newDrawer = new Drawer({ socks: [], timesOpened: 0});
Run Code Online (Sandbox Code Playgroud)
我正在考虑在我的资源(我称之为drawerService)中使用相同的服务定义一个默认/初始化对象,如下所示:
defaultDrawer = { socks: [], timesOpened: 0};
Run Code Online (Sandbox Code Playgroud)
然后在创建资源时:
//New drawer with defaults
var newDrawer = new drawerService.Drawer(drawerService.defaultDrawer);
//New drawer with defaults and some other initialization
var anotherDrawer = new drawerService.Drawer(angular.extend(drawerService.defaultDrawer, {material: 'plastic'});
Run Code Online (Sandbox Code Playgroud)
这是唯一的方法吗?
在Chrome上使用调试时,它会在angular.js:36上显示此错误:未捕获的对象.
我究竟做错了什么?:/
谢谢你的帮助 :)
模块和资源对象(services.js)
var services = angular.module('ngdemo.services', ['ngResource']);
services.factory('ProvidersFactory', function ($resource) {
return $resource('http://devfz.azurewebsites.net/api/providers/', {}, {
query: { method: 'GET', isArray: true },
})
});
Run Code Online (Sandbox Code Playgroud)
控制器(controller.js)
var app = angular.module('ngdemo.controllers', []);
app.controller('ProviderListCtrl', ['$scope', 'ProvidersFactory', '$location',
function ($scope, ProvidersFactory, $location) {
$scope.providers = ProvidersFactory.query();
}]);
Run Code Online (Sandbox Code Playgroud)
app.js
angular.module('ngdemo', ['ngdemo.filters', 'ngdemo.services', 'ngdemo.directives', 'ngdemo.controllers']).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/provider-list', { templateUrl: '/provider-list.html', controller: 'ProviderListCtrl' });
$routeProvider.otherwise({ redirectTo: '/provider-list' });
}]);
Run Code Online (Sandbox Code Playgroud)
HTML
<!DOCTYPE html>
<html ng-app="ngdemo" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Providers</title>
<link href="Content/bootstrap.min.css" rel="stylesheet">
<script …Run Code Online (Sandbox Code Playgroud) 我最近才开始学习ng-resource.我想知道在将常规AngularJS应用程序转换为使用ng-resource的应用程序时,我是否在正确的轨道上.
这是我的旧home.js控制器(不使用ng-resource):
angular.module("HomeApp", ["BaseApp"])
.controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) {
var self = this;
BaseService.fetch.posts(function() {
self.posts = BaseService.posts;
self.cerrorMessages = BaseService.cerrorMessages;
});
self.like = function(id, postType) {
BaseService.like(id, postType, function() {
self.cerrorMessages = BaseService.cerrorMessages;
});
};
}]);
Run Code Online (Sandbox Code Playgroud)
这是我的旧base.js(BaseService):
angular.module("BaseApp", [])
.factory("BaseService", ["$http", "$window", function($http, $window) {
var self = this;
self.posts = [];
self.cerrorMessages = [];
self.accessErrors = function(data) {
self.cerrorMessages = [];
for (prop in data) {
if (data.hasOwnProperty(prop)){
self.cerrorMessages.push(data[prop]);
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个$resourceAPI将始终返回一些需要在进入表示层之前清理的数据.具体来说,它是.NET以可爱的'/Date(...)/'格式返回Date对象.
我不想每次打电话.query()或写电话都要写回电.get().是否有某种方法可以使用一个回调来扩展资源,该回调可以在更新实例属性的REST方法上调用,或者$watch在date属性更改时添加一些被触发的类型?基本上每件事都会发生$resource.
angular.module('myAppServices', ['ngResource'])
.factory('Participant', ['$resource', function ($resource) {
var res = $resource('api/url/participants/:id', { id: '@id' });
// This obviously doesn't work, but something kinda like this?
res.prototype.$watch(this.FieldName, function(newVal, oldVal) {
if (needsCleaning(newVal.fieldName) {
this.FieldName = cleanupField(newVal);
}
};
});
Run Code Online (Sandbox Code Playgroud) 我使用ng-resource从我的服务器获取数据,然后将数据放入表格中,如下所示:
<div ng-form name="grid">
<button type="submit" data-ng-disabled="grid.$pristine">Save</button>
<div class="no-margin">
<table width="100%" cellspacing="0" class="form table">
<thead class="table-header">
<tr>
<th>ID</th>
<th>Title</th>
</tr>
</thead>
<tbody class="grid">
<tr data-ng-repeat="row in grid.data">
<td>{{ row.contentId }}</td>
<td><input type="text" ng-model="row.title" /></td>
</tr>
</tbody>
</table>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
有没有办法让我这样做,以便单击Submit按钮检查网格中是否有更改的行,然后调用以putEntity(row)行作为参数的函数?
ngresource ×10
angularjs ×9
javascript ×4
angular ×1
cors ×1
express ×1
node.js ×1
rest ×1
restangular ×1