我有一个资源定义如下:
app.factory("DatumItem", function($resource) {
return $resource('/data/:id', {id: '@id'});
});
Run Code Online (Sandbox Code Playgroud)
在我看来,我有:
<div ng-click="go('/datum/' + d.to_param)">Test</div>
Run Code Online (Sandbox Code Playgroud)
其中go()在我的控制器中定义为:
$scope.go = function (params) {
$location.path(params);
};
Run Code Online (Sandbox Code Playgroud)
对于有问题的项目,d.param等于
TkZUOWZwcnc9Uldo%0ASzRvd2FiWk
Run Code Online (Sandbox Code Playgroud)
但是当我用正确的ID调用DatumItem.get()时,它正在将id更改为
TkZUOWZwcnc9Uldo%250ASzRvd2FiWk
Run Code Online (Sandbox Code Playgroud)
在这种情况下,有没有办法阻止%被编码为%25?
我尝试过使用encodeURI,encodeURIComponent的组合无济于事.
任何帮助将不胜感激,谢谢!
假设我有一个待办事项应用程序并单击任何单个Todo上的复选框将其标记为完成并执行PUT操作.
然后有一个"标记全部完成"或"标记全部不完整"的复选框.这应该标记每个todo已完成/不完整,无论其个人状态如何.
使用时angular-resource,更新所有项目的最佳实践方法是什么.是否可以在单个批量请求中执行此操作并更新所有项目?或者我会更好地单独更新每一个?
使用角度1.1.5并需要将urlencoded数据传递给后端.我从这里开始使用解决方案: 如何将数据作为表单数据而不是请求有效负载发布?
$http({
method: 'POST',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: xsrf
}).success(function () {});
Run Code Online (Sandbox Code Playgroud)
我已经成功地将它嵌入到我的控制器中,但"更清洁"的方式是使用服务和$ resource而不是$ http对象.在本主题的1.1.2之后可以使用transformRequest和$ resource: $ resource transformResponse不工作 但我找不到任何有效的例子.任何人都可以使用$ resource作为服务对象提供上述解决方案的示例吗?
我有一个基本的角度资源(角度1.0.7):
app.factory "User", [ "$resource", ($resource)->
$resource "/users/:id", { id: '@id' }, {
index: { method: 'GET', isArray: false }
}
]
Run Code Online (Sandbox Code Playgroud)
我可以传递如下参数:
User.index({ foo: 1, bar: 2 })
Run Code Online (Sandbox Code Playgroud)
但我需要传递嵌套参数:
User.index({ foo: { bar: 1 } })
Run Code Online (Sandbox Code Playgroud)
这失败了因为它发送:
/users?foo=%5Bobject+Object%5D
Run Code Online (Sandbox Code Playgroud)
我试过了:
User.index({ foo: JSON.stringify({ bar: 1 }) })
Run Code Online (Sandbox Code Playgroud)
但显然在服务器端(仅仅是字符串)无法识别参数,我想避免在那里解析的麻烦.
你对这个问题有一个优雅的解决方案吗?
使用jQuery我已经完成了:
$.get("/users", { foo: { bar: 1 } } )
Run Code Online (Sandbox Code Playgroud)
生产:
/users?foo%5Bbar%5D=1
Run Code Online (Sandbox Code Playgroud)
完美地由服务器解释.
我正在使用Angular js来显示加载屏幕.它适用于除REST服务之外的所有REST服务调用以下载文件.我理解为什么它不起作用,因为下载我没有使用$ resource进行任何服务调用; 而不是我使用正常的方法下载文件因此Angular js代码没有任何控制启动/完成服务请求.我尝试使用$ resource来访问这个REST服务但是我从这个服务获取数据,在这种情况下,加载屏幕工作正常,但不确定如何使用此数据显示给用户以角度方式下载.以下是必需的细节.请帮忙.
方法1使用iframe方法:
/*Download file */
scope.downloadFile = function (fileId) {
//Show loading screen. (Somehow it is not working)
scope.loadingProjectFiles=true;
var fileDownloadURL = "/api/files/" + fileId + "/download";
downloadURL(fileDownloadURL);
//Hide loading screen
scope.loadingProjectFiles=false;
};
var $idown; // Keep it outside of the function, so it's initialized once.
var downloadURL = function (url) {
if ($idown) {
$idown.attr('src', url);
} else {
$idown = $('<iframe>', { id: 'idown', src: url }).hide().appendTo('body');
}
};
Run Code Online (Sandbox Code Playgroud)
方法2使用$ resource(不确定如何在屏幕上显示数据下载)
/*Download file …Run Code Online (Sandbox Code Playgroud) 我有一个RESTful与应用程序Laravel 4和Angular JS.
在我的Laravel控制器中,
public function index() {
$careers = Career::paginate( $limit = 10 );
return Response::json(array(
'status' => 'success',
'message' => 'Careers successfully loaded!',
'careers' => $careers->toArray()),
200
);
}
Run Code Online (Sandbox Code Playgroud)
和角度剧本,
var app = angular.module('myApp', ['ngResource']);
app.factory('Data', function(){
return {
root_path: "<?php echo Request::root(); ?>/"
};
});
app.factory( 'Career', [ '$resource', 'Data', function( $resource, Data ) {
return $resource( Data.root_path + 'api/v1/careers/:id', { id: '@id'});
}]);
function CareerCtrl($scope, $http, Data, Career) {
$scope.init …Run Code Online (Sandbox Code Playgroud) laravel angularjs angular-resource laravel-4 angularjs-scope
我试图将数据分配给$ scope变量.在我的$ promise.then()函数中,它正确显示但在函数外部显示为undefined.以下是我的控制器代码:
angular.module('testSiteApp').controller('TestController', function ($scope, Tests) {
$scope.test = Tests.get({id: 1});
$scope.test.$promise.then(function(data) {
$scope.tasks = data.tasks;
console.log($scope.tasks);
});
console.log($scope.tasks);
});
Run Code Online (Sandbox Code Playgroud)
then()函数内的结果:
[Object, Object, Object, Object]
Run Code Online (Sandbox Code Playgroud)
then()函数之外的结果:
undefined
Run Code Online (Sandbox Code Playgroud)
我正在使用的'Tests'服务工厂如下:
angular.module('testSiteApp').factory('Tests', function($resource) {
return $resource('/api/test/:id', {id: '@id'}, { 'update': { method: 'PUT' } } );
});
Run Code Online (Sandbox Code Playgroud)
即使我使用查询方法而不是get for my资源并将isArray设置为true,我仍然会遇到同样的问题.由于某种原因,数据没有绑定到then函数内的我的范围.
我很抱歉,如果这是一个重复的问题,但我到处寻找,只发现与$ promise函数有关的未定义问题,在这种情况下不是问题.
在此先感谢您的支持.
javascript angularjs angular-resource angularjs-scope angular-promise
我从服务器返回一个对象数组:
[{id: 1, name: "name"},{id: 2, name: "name2"}]
Run Code Online (Sandbox Code Playgroud)
现在我使用angular-resource $query来获取数据,因为它需要一个数组.收到数据后,我收到此错误:
TypeError: value.push is not a function
Run Code Online (Sandbox Code Playgroud)
我从server =给出的响应有问题吗?
错误来源:
// jshint +W018
if (action.isArray) {
value.length = 0;
forEach(data, function(item) {
if (typeof item === "object") {
value.push(new Resource(item));
} else {
// Valid JSON values may be string literals, and these should not be converted
// into objects. These items will not have access to the Resource prototype
// methods, but unfortunately there
value.push(item);
}
});
} else …Run Code Online (Sandbox Code Playgroud) 我正在创建一个应用程序使用角度和打字稿.一切都很好地融合在一起,但有一个问题让我感到困惑.
我定义了我想在应用程序中传递的实体/模型类,类的数据来自$ resource调用的JSON.
以下是模型类的示例:
module app.domain {
export interface IJob {
id: number;
jobTitle: string;
jobDescription: string;
}
export class Job implements IJob {
constructor(public id:number, public jobTitle: string, public jobDescription: string) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
我通过返回资源的服务访问我的JSON资源:
namespace app.services {
'use strict';
interface IDataService {
getJobResource(): angular.resource.IResourceClass<IJobResource>
}
interface IJobResource extends angular.resource.IResource<app.domain.IJob>{
}
export class DataService implements IDataService {
static $inject: Array<string> = ['$log','$resource','ApiDataEndpoint'];
constructor(
private $log: angular.ILogService,
private $resource: angular.resource.IResourceService,
private ApiDataEndpoint
){}
getJobResource(): angular.resource.IResourceClass<IJobResource>{
return this.$resource(this.ApiDataEndpoint.url + 'job/:id',{}); …Run Code Online (Sandbox Code Playgroud) Angular返回控制台错误
encodeUriSegment is not a function
当我在自己的控制器中尝试从angular-resource调用get函数时,就会发生这种情况.看起来像angular-resource找不到这个函数,但它存在于angular.js文件中.我用凉亭安装了角度资源.我在index.html中拥有所需的每个文件路径.
完整的错误消息:
angular.js:13920 TypeError: [horoskopy_App Error] encodeUriSegment is not a function
at http://localhost:3000/bower_components/angular-resource/angular-resource.js:573:30
Run Code Online (Sandbox Code Playgroud) angular-resource ×10
angularjs ×9
javascript ×4
angular-http ×1
download ×1
json ×1
laravel ×1
laravel-4 ×1
post ×1
typescript ×1
urlencode ×1