我试图在用户与之交互时保持最新的资源列表.使用AngularJS ngResource我最初使用它的query方法获取列表.然后每个资源都有一个$remove(或$delete)方法,对吗?但是当被触发时,资源不会从返回的列表中删除query.
我知道这很多,但我几乎希望它会为我做一切.除此之外,我怎么能做到这一点.资源本身是否会发出某种事件?它有删除的属性$watch吗?我怎么知道资源是什么,$remove所以我可以把它从列表中拼凑出来?
谢谢.
我有一个服务栈REST服务和一个angular.js客户端.Web托管在另一个端口上,然后是RESTService.
使用$ http检索数据但使用$ resource不起作用.
在REST服务中,我设置了以下全局标头:
GlobalResponseHeaders =
{
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
{ "Access-Control-Allow-Headers", "X-Requested-With"}
}
Run Code Online (Sandbox Code Playgroud)
现在在angular.js中我可以读取成功读取$ http的方法
$http({
method:'get',
url:address,
requesttype:'application/json'
}).success(function(data) {
$scope.ServiceStatus = data;
});
Run Code Online (Sandbox Code Playgroud)
但使用$ ressource读取相同的地址不起作用我的工厂代码是:
myApp.factory('qaServiceStatus', function($resource){
return $resource('http://localhost:1338/api/status', {}, {
query: {method:'GET', headers: {'Content-Type': 'application/json'}, params:{}}
});
});
Run Code Online (Sandbox Code Playgroud)
我在控制器中以这种方式使用它
$scope.RESTStatus = qaServiceStatus.get({});
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:http://localhost:7000Access-Control-Allow-Origin不允许Origin.
和XMLHttpRequest无法加载http://localhost/api/status.原产地http://localhost:7000不被访问控制允许来源允许的.
我究竟做错了什么?
我也尝试将允许的原始内容更改为确切的地址,但这没有用
我正在调用一个我无法控制的后端服务器.目前它正在使用这样的jQuery ajax:
return $.ajax({
type: "POST",
url: "/api/cases/store",
contentType: "application/json",
data: JSON.stringify(parameters),
headers: { "Authorization": cred } : {}
}) // ... etc.
Run Code Online (Sandbox Code Playgroud)
我想将它转换为使用该$resource服务,并让它工作这样做
$http.defaults.headers.common['Authorization'] = cred;
return $resource('/api/cases/store').save();
Run Code Online (Sandbox Code Playgroud)
唯一的问题是我必须$http使用auth凭据设置全局服务默认值.
我看到你应该能够通过$http调用传递自定义标头,现在通过$resource调用,但我找不到任何关于如何在我的情况下执行它的示例(使用POST).
我在AngularJS文档中也找不到任何相关内容.你们怎么想出这个东西?文档非常糟糕!
javascript http-post angularjs angularjs-resource angularjs-http
我有这个工厂:
factory('getJson', ['$resource', function($resource) {
return $resource('json/greetings.json', {}, {
query: {method:'GET', isArray:true}
});
}]);
Run Code Online (Sandbox Code Playgroud)
该文件是硬编码的'greetings.json',我希望工厂根据我视图中的复选框获取文件:
<li><input type="checkbox" ng-model="includeVeggies" />Veggies</li>
<li><input type="checkbox" ng-model="includeGreetings" />Greetings</li>
Run Code Online (Sandbox Code Playgroud)
知道我该怎么做?
如何在不刷新页面的情况下刷新AngularJS中$ resource的查询结果?
我的页面:
<div class="container" ng-controller="AppController">
<div ng-repeat="tile in items">
<p>{{tile.name}}</p>
</div>
<button ng-click='??'> Reload tiles </button>
</div>
Run Code Online (Sandbox Code Playgroud)
控制器:
(function(angular) {
var AppController = function($scope, Item) {
Item.query(function(response) {
$scope.items = response ? response : [];
});
};
AppController.$inject = ['$scope', 'Item'];
angular.module("myApp.controllers").controller("AppController", AppController);
}(angular));
Run Code Online (Sandbox Code Playgroud)
我知道这可能是非常棒的问题但是我找不到任何解决方案(今天是我与AngularJS的第一天,所以任何帮助都表示赞赏).
我试图解雇中止请求,但我没有得到$cancelRequest结果的对象$resource.但是,我能够得到$promise和$resolved对象.
为什么会这样?我怎么能得到这个$cancelRequest?
PS:我正在使用AngularJS 1.5
更新:经过一些试验和错误后我发现它不起作用只是因为我使用的是AngularJS 1.5 rc 0.现在当我使用AngularJS 1.5 rc 2.这是当前的最新版本时,它只是起作用.
我正在测试登录和注销功能,下面是我的测试用例
it('should redirect to login page on click of logout',function(){
signInPage.email.sendKeys('zahid.afaque@meltwater.com');
signInPage.password.sendKeys(1234);
signInPage.loginButton.click();
expect(browser.getCurrentUrl()).toMatch(/\/collections/);
signInPage.profileImage.click();
signInPage.logout.click();
browser.waitForAngular();
expect(browser.getCurrentUrl()).toMatch(/\/login/);
});
Run Code Online (Sandbox Code Playgroud)
当我在测试上运行时,它的失败和下面是错误消息
Error: Timed out waiting for Protractor to synchronize with the page after 11001ms. Please see https://github.com/angular/protractor/blob/master/docs/faq.md
Run Code Online (Sandbox Code Playgroud)
我尝试使用,browser.waitForAngular();但它没有帮助.当我删除期望"expect(browser.getCurrentUrl()).toMatch(/\/login/);"然后它已经过去了.任何人遇到同样的问题,任何帮助都是适当的
javascript jasmine angularjs-resource angularjs-e2e protractor
我有这样配置的标准angular $ resource
angular.module('client.resources')
.factory('ProjectSubjectResource',['$resource',
function ($resource) {
release: {
method: 'DELETE',
isArray: false
}
});
}]);
Run Code Online (Sandbox Code Playgroud)
我称这种方法为
ProjectSubjectResource.release({projectId: projectId, subjectId: 0},{ subjectIds: subjectIdArray})
Run Code Online (Sandbox Code Playgroud)
其中subjectIdArray是对象数组:
[{subject1: 213123}, {subject2: 3131}]
Run Code Online (Sandbox Code Playgroud)
但是,请求主体不包含该数组.我怀疑DELETE请求是问题,因为重命名方法调用例如PUT有所不同.
我可以以某种方式允许DELETE请求的主体吗?
资源:
angular.module('TicketService', ['ngResource'])
.factory('Ticket', ['$resource', function($resource){
var Ticket = $resource('/api/tickets/:id1/:action/:id2',
{
id1:'@id'
},
{
list: {
method: 'GET'
},
listByOwner: {
method: 'GET',
params: {
action:'owner',
id1:"@id"
}
}
update: {
method: 'PUT',
params:{}
}
});
return ticket;
}]);
Run Code Online (Sandbox Code Playgroud)
查询:
$scope.userTickets = Ticket.listByOwner({
id : $rootScope.user.id
}, function(){
//success
}, function(response){});
Run Code Online (Sandbox Code Playgroud)
结果:

Angularjs构建了一个错误的URL,/api/tickets但它应该是/api/tickets/2/owner.有什么想法吗?
我提出要求的工厂在这里:
angular.module('myapp').factory('testResponse',
['$http', '$resource', 'AppConfig', '$routeParams', '$rootScope',
function($http, $resource, $routeParams, $rootScope) {
$http.defaults.headers.common['Authorization'] = authorizationHeader;
$http.defaults.headers.post['Content-Type'] = 'application/json';
return $resource('test.json'), {}, {
query: {method: 'GET'}
};
}]);
Run Code Online (Sandbox Code Playgroud)
控制器中的代码在这里:
angular.module('myapp').controller('TestCtrl',
['$http', '$scope', 'testResponse', 'AppConfig', function TestCtrl($http, $scope, testResponse) {
testResponse.query(function(data) {
console.log(data.status);
})
}]);
Run Code Online (Sandbox Code Playgroud)
理想情况下,它应该记录 $http 请求中的状态,但我无法通过 $reource 获取它
我正在开发一个页面,它使用Angularjs和nodejs显示20个产品详细信息的列表.我有REST API来分别获取每个产品的数据.这是我在启动其他页面时使用的相同服务,其中显示了该产品的详细信息.
我正在寻找解决方案,以便在angularjs客户端代码中,我可以对REST API进行20次点击并组合数据,然后使用angularjs指令在我的页面中显示20个产品的列表.
一旦客户端获取所有数据然后将其组合然后在UI中显示,我如何并行地进行20个REST API调用以获取数据.
目前我正在逐个拨打电话并合并数据.以下是我正在使用的代码段.我不喜欢这种方法,因为它看起来根本不是最优的.
ApplicationLoaderService.initializePageCache($scope, 'product_1', 'myPage', function (response) {
updateCache('product_1', 'myPage', response);
ApplicationLoaderService.initializePageCache($scope, 'product_2', 'myPage', function (response) {
updateCache('product_2', 'myPage', response);
ApplicationLoaderService.initializePageCache($scope, 'product_3', 'myPage', function (response) {
updateCache('product_3', 'myPage', response);
ApplicationLoaderService.initializePageCache($scope, 'product_4', 'myPage', function (response) {
updateCache('product_4', 'myPage', response);
ApplicationLoaderService.initializePageCache($scope, 'product_5', 'myPage', function (response) {
updateCache('product_5', 'myPage', response);
ApplicationLoaderService.initializePageCache($scope, 'product_6', 'myPage', function (response) {
updateCache('product_6', 'myPage', response);
ApplicationLoaderService.initializePageCache($scope, 'product_7', 'myPage', function (response) {
updateCache('product_7', 'myPage', response);
ApplicationLoaderService.initializePageCache($scope, 'product_8', 'myPage', function (response) {
updateCache('product_8', 'myPage', response);
ApplicationLoaderService.initializePageCache($scope, …Run Code Online (Sandbox Code Playgroud) 我是Angular的新手,并试图在我的Rails应用程序上使用它.但是我得到了以下错误.
TypeError:无法读取未定义的属性"index"
app.js代码
angular.module('ticket', ['ngResource', 'ui.router', 'templates'])
.controller('AppCtrl', ['$rootScope', function($rootScope) {
var _this = this;
return $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
return $rootScope.pageTitle = $route.current.title;
});
}])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'home/_home.html',
controller: 'MainCtrl'
})
.state('members', {
url: '/members',
templateUrl: 'members/_members.html',
controller: 'MemberCtrl'
})
.state('guards', {
url: '/guards',
templateUrl: 'guards/_guards.html',
controller: 'GuardCtrl'
})
.state('tickets', {
url: '/tickets',
templateUrl: 'tickets/_tickets.html',
controller: 'TicketCtrl'
});
$urlRouterProvider.otherwise('home')
}]);
Run Code Online (Sandbox Code Playgroud)
memberCtrl.js代码(内部成员目录)
angular.module('ticket')
.factory('Member', ['$resource', function($resource) {
return $resource("/members/:id", {id: …Run Code Online (Sandbox Code Playgroud) angularjs ×12
javascript ×4
rest ×2
asynchronous ×1
cors ×1
http-post ×1
jasmine ×1
jquery ×1
protractor ×1