如何传递一个控制器变量作为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)[2023-02-22]
你的物体可能非常大! 查询字符串对您可以通过基于浏览器的数据传递的数据量有限制。 你应该考虑传递一个唯一的id值(记录),并使用它在你的action方法中从db获取整个记录并将其传递给视图。 @foreach(var item in SomeCollection) {@Html.Action("Update me!", "Update", new { id = item.Id }) -
你可以把它传递给视图吗? 是的,但您的观点将不会被强制输入。 但是帮助者会工作。 例如: public ActionResult Foo() { return View(new {Something="Hey, it worked!"}); } //Using a normal ViewPage <%= Html.TextBox("Something") %> 该文本框应该呈现“嘿,它的工作! 作为价值。 那么你可以定义一个视图,其中T被从控制器传递给它的是什么定义的? 好的,但是在编译时显然不是 ...
-
您可以在ASP.NET MVC中使用部分视图来获取类似的行为。 部分视图仍然可以在服务器上构建HTML,您只需要将HTML插入适当的位置(实际上,如果您愿意包含MSFT Ajax库,则MVC Ajax帮助者可以为您设置此选项)。 在主视图中,您可以使用Ajax.Begin窗体来设置异步请求。 <% using (Ajax.BeginForm("Index", "Movie", new AjaxOptions { ...
-
已经在ASP.NET MVC的插件体系结构中得到了解答 也可以看看: ASP.NET MVC插件 插件混合:ASP.NET WebForms和ASP.MVC与ASP.NET动态数据并排 ASP.NET MVC - 如何实现插件体系结构 Already answered in Plug-in architecture for ASP.NET MVC See Also: ASP.NET MVC Plugins Plug-In Hybrids: ASP.NET WebForms and ASP.MVC and A ...
-
ASP.Net实体框架(ASP.Net Entity Framework)[2022-08-20]
对于任何感兴趣的人,我使用以下代码解决了这个问题 UnityGamersEntities db2 = new UnityGamersEntities(); ObjectQuerygc = db2.GameCalendar.Include("GameTitles"); 似乎真的缺乏实体框架的教程,除非你只想使用单个表,我发现很难找到我需要的信息。 希望这将在未来几个月内改变。 for anyone interested I solved this ... -
实际上,我将ASP.NET MVC看作下一代,因为它是一种进化 - 试图成为更好的编程环境,因为针对Web应用程序的软件开发要求更多的可测试性。 这是一个巨大的野兽。 根据功能决定是否需要它。 MVC文档较少,而且由于采用了较少的RAD方法,因此很难掌握,但似乎一旦进入,这将是一次相当好的体验。 如果你有一个web应用程序(如stackoverflow.com),那么它可能是一个好方法。 DevExpress组件 - 玩得开心...把它们扔掉。 像大多数ASP.NET组件一样,它们不能工作或只能部分工作。 ...
-
您将发现使用MVC而非WebForms的许多差异和原因,但这取决于这些差异对您或您的项目是否重要。 路由绝对是MVC的一大优势。 虽然您可以在WebForms中实现自定义路由配置,但它的直观性却大大降低。 另一大优势是单元测试。 与IoC容器和模拟框架一起,MVC使单元测试变得简单。 使用MVC隔离操作和行为并且具体地测试它们要容易得多。 第三个优点是MVC将有助于减少你要编写的意大利面条代码。 如果您不打算使用任何用户控件,那么在您错过MVC中的HTML帮助程序之前不久。 Html,Url,ViewMod ...
-
你在这里列举了三件事: 网站统计 动态菜单 无论其他什么[...] 可能来自数据库 第三点是陷阱; 仅因为某些内容存在于您的数据库中, 并不意味着它是您的域模型的一部分。 导航可能基于数据库。 它也可能位于站点地图中或硬编码到应用程序中。 它实际上不是应用程序数据 ,而是应用程序配置 。 如果你将它存储在应用程序数据库中,那就没关系(好吧,不是真的),但它不是模型本身的一部分。 同样,站点统计信息通常存储在数据库中,但它们存储在不同的数据库中,特别是分析数据库(许多人只是将其“外包”给Google)。 同样 ...
-
asp.net mvc日志框架(asp.net mvc logging framework)[2022-03-11]
我个人喜欢让不同的系统记录不同类型的数据。 正如我所看到的,每个都是日志记录角色的不同领域。 分析师不应该关心#1异常,这些异常适用于开发人员,开发人员应该关注#2(Google Analytics或Adobe SiteCatalyst)。 我觉得允许不同的系统根据需要进行日志记录并以不同方式分发日志记录信息是合适的,尽管如你所说它有点多。 这个问题应该在Programmers.StackExchange.Com上提出。 Personally I like having different system l ... -
我们如何在ASP.NET mvc框架中将数据从一个视图传递到另一个视图(How we can pass data from one view to another in ASP.NET mvc framework)[2023-05-14]
您可以使用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. Querystrin ...