如何传递一个控制器变量作为AngularJS $资源请求的参数?(How to pass a controller variable as a parameter for a AngularJS $resource request?)
我是angularjs的新手,我认为这应该是一件简单的事情,但我无法完成它。 基本上我需要的是从一个服务获取一个变量并将其作为GET请求中的参数传递。 为此,我使用ngResource模块。
我从服务中获得的变量可能会发生变化,我需要的变量也是更改发出请求的url以及将呈现并显示在视图中的数据。
这就是为什么我需要获取该变量,将其传递给控制器并使用服务中的param定义在此控制器中生成$ resource请求。
这是我的控制器:
.controller('LicoresController',['$scope','LicoresService','CategoriaService',function($scope,LicoresService,CategoriaService){ $scope.licores=[]; $scope.pk=CategoriaService.getPk(); console.log($scope.pk); LicoresService.query({disponibles:$scope.pk},function(data){ $scope.licores=data; console.log(data); },function(error){ console.log(error); }); }]);
LicoresService
.factory('LicoresService',['$resource',function($resource){ return $resource('http://192.168.0.16:8000/api/productos/',{licorera:2},{isArray:true}); }]);
和CategoriaService(从这一个,我得到我需要作为请求的参数传递的变量)
.factory('CategoriaService',function(){ var pk=0; function getPk(){ return pk; } function setPk(newpk){ pk=newpk; } return{ setPk:setPk, getPk:getPk, }; });
在视图中,我使用ng-repeat指令迭代$ scope.licores数组。
<li ng-repeat="licor in licores"> <img src="{{licor.imagen}}"/> <table align="center"> <tr><td >{{licor.marca|uppercase}}</td></tr> <tr><td >Tamaño</td><td >{{licor.tamano}}</td></tr> <tr> <td>Precio<br/> <span >$ {{licor.precio}}</span> </td> </tr> </table> </li>
问题似乎是LicoresService.query()函数没有更新视图。 因此我认为每次$ scope.pk变量改变其值时我都需要调用该函数。 但我不知道该怎么做。
I am new in angularjs and I think this should be a simple thing to do but I could not accomplish it. Basically what I need is to get a variable from an service and pass it as a parameter in a GET request. For this I am using the ngResource module.
The variable I get from the Service may change and what i need , likewise, is to change the url that made the request as well as the data that would render and be show in the view.
That's why i need to get that variable, pass it to a controller and make the $resource request in this controller with the param define in the service.
This is my controller:
.controller('LicoresController',['$scope','LicoresService','CategoriaService',function($scope,LicoresService,CategoriaService){ $scope.licores=[]; $scope.pk=CategoriaService.getPk(); console.log($scope.pk); LicoresService.query({disponibles:$scope.pk},function(data){ $scope.licores=data; console.log(data); },function(error){ console.log(error); }); }]);
LicoresService
.factory('LicoresService',['$resource',function($resource){ return $resource('http://192.168.0.16:8000/api/productos/',{licorera:2},{isArray:true}); }]);
And CategoriaService (from this one, i get the variable that i need to pass as a param for the request)
.factory('CategoriaService',function(){ var pk=0; function getPk(){ return pk; } function setPk(newpk){ pk=newpk; } return{ setPk:setPk, getPk:getPk, }; });
In the view i use the ng-repeat directive to iterate through the $scope.licores array.
<li ng-repeat="licor in licores"> <img src="{{licor.imagen}}"/> <table align="center"> <tr><td >{{licor.marca|uppercase}}</td></tr> <tr><td >Tamaño</td><td >{{licor.tamano}}</td></tr> <tr> <td>Precio<br/> <span >$ {{licor.precio}}</span> </td> </tr> </table> </li>
The problem seems to be that the LicoresService.query() function is not updating the view. So I think that i need to call that function every time that the $scope.pk variable change his value. But i don´t know how to do it.
原文:https://stackoverflow.com/questions/35117743
满意答案
您可以使用ASP.net中提供的所有常规数据传输方法
客户端:1。Cookie 2. Querystring
服务器端:1。会话
有关这些的更多信息,请参阅http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx
谢谢,Ashwani
You can all conventional data transfer method which are available in ASP.net
Client Side: 1. Cookie 2. Querystring
Server side: 1. Sessions
For more information on these see http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx
Thanks, Ashwani
相关问答
更多在ASP.NET MVC 5中将整个对象从视图传递到控制器(Pass entire object from view to controller in ASP.NET MVC 5)
我可以将匿名类型传递给我的ASP.NET MVC视图吗?(Can I pass an anonymous type to my ASP.NET MVC view?)
如何在ASP.NET MVC中制作更新面板(How to make update panel in ASP.NET MVC)
在asp.net mvc中创建“可插入”应用程序(Creating “pluggable” apps in asp.net mvc)
ASP.Net实体框架(ASP.Net Entity Framework)
Asp.net MVC VS ASP.net WebForms?(Asp.net MVC VS ASP.net WebForms?)
asp.net MVC框架的有用性,而不是使用常规asp.net编写MVC样式?(Usefulness of asp.net MVC framework as opposed to coding MVC style with regular asp.net?)
ASP.NET MVC - 查看模型模式(ASP.NET MVC - View Model pattern)
asp.net mvc日志框架(asp.net mvc logging framework)
我们如何在ASP.NET mvc框架中将数据从一个视图传递到另一个视图(How we can pass data from one view to another in ASP.NET mvc framework)
相关文章
更多Becoming a data scientist
[转]So You Want To Be A Producer
Solr: a custom Search RequestHandler
按钮样式
Spring Data: a new perspective of data operations
Create a Bootable MicroSD Card
AngularJS资源
Drupal Forums instead of phpBB or vBulletin: A casestudy
Python内建函数(A)
HTML 超链接(a标签、锚)
最新问答
更多sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
湖北京山哪里有修平板计算机的
SimplePie问题(SimplePie Problem)
在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
为什么我会收到链接错误?(Why do I get a linker error?)
如何正确定义析构函数(How to properly define destructor)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!