首页 \ 问答 \ 如何传递一个控制器变量作为AngularJS $资源请求的参数?(How to pass a controller variable as a parameter for a AngularJS $resource request?)

如何传递一个控制器变量作为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
更新时间:2024-04-23 17:04

最满意答案

您可以使用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

相关问答

更多
  • 你的物体可能非常大! 查询字符串对您可以通过基于浏览器的数据传递的数据量有限制。 你应该考虑传递一个唯一的id值(记录),并使用它在你的action方法中从db获取整个记录并将其传递给视图。 @foreach(var item in SomeCollection) { @Html.Action("Update me!", "Update", new { id = item.Id }) } 并在行动方法 public ActionResult U ...
  • 你可以把它传递给视图吗? 是的,但您的观点将不会被强制输入。 但是帮助者会工作。 例如: 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 ...
  • 对于任何感兴趣的人,我使用以下代码解决了这个问题 UnityGamersEntities db2 = new UnityGamersEntities(); ObjectQuery gc = 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)。 同样 ...
  • 我个人喜欢让不同的系统记录不同类型的数据。 正如我所看到的,每个都是日志记录角色的不同领域。 分析师不应该关心#1异常,这些异常适用于开发人员,开发人员应该关注#2(Google Analytics或Adobe SiteCatalyst)。 我觉得允许不同的系统根据需要进行日志记录并以不同方式分发日志记录信息是合适的,尽管如你所说它有点多。 这个问题应该在Programmers.StackExchange.Com上提出。 Personally I like having different system l ...
  • 您可以使用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 ...

相关文章

更多

最新问答

更多
  • CSS修复容器和溢出元素(CSS Fix container and overflow elements)
  • SQL多个连接在与where子句相同的表上(SQL Multiple Joins on same table with where clause)
  • nginx 80端口反向代理多个域名,怎样隐藏端口的
  • xcode提醒样式,swift 3(xcode alert style, swift 3)
  • 在Chrome控制台中调试JavaScript(debugging javascript in Chrome console)
  • Javascript - 试图围绕自定义事件(Javascript - Trying to wrap my head around custom events)
  • 边栏链接不可点击(Sidebar links aren't clickable)
  • 使用recpatcha gem时如何显示其他表单错误?(How do I display other form errors when using the recpatcha gem?)
  • boost.python避免两次注册内部类,但仍然在python中公开(boost.python Avoid registering inner class twice but still expose in python)
  • Android 现在软件很少吗?以后会多起来吗
  • 如何在ActiveAdmin 0.5.0中为资源全局指定预先加载?(How to specify eager loading globally for a resource in ActiveAdmin 0.5.0?)
  • matlab代码为黄金比例持续分数(matlab code for golden ratio continued fraction)
  • Android浏览器触摸事件位置(Android browser touch event location)
  • 将cURL输出分配给Bash中的变量(Assign output to variable in Bash)
  • 我如何在MVC视图上没有时间获取当前日期(how i can get current date without time on MVC view)
  • sql连接函数(sql join of function)
  • 为什么在Xamarin Media插件中使用ImageSource.FromStream而不是FromFile?(Why use ImageSource.FromStream instead of FromFile in Xamarin Media plugin?)
  • 这段代码是否真的可以防止SQL注入?(Will this code actually work against SQL-injection? [duplicate])
  • 信阳方远计算机学校大专证被国家认可么
  • React / Rails AJAX POST请求返回404(React/Rails AJAX POST request returns 404)
  • Android与php服务器交互(Android interact with php server)
  • 自动刷新QTableWidget可能吗?(Refresh QTableWidget automatically possible?)
  • JVM / Compiler优化对象的未使用属性(optimization of unused properties of object by JVM / Compiler)
  • 插入表格时,乌克兰字符会更改为问号(Ukrainian character change to question mark when insert to table)
  • 在头文件中包含异常类(Including an exception class in a header file)
  • 完成c#中的执行后关闭sqlcmd(Close sqlcmd after finishing executing in c#)
  • 使用软导航栏正确检测屏幕尺寸(Detecting screensize correctly with soft navigation bar)
  • Typescript:从输入更新值(Typescript : update value from input)
  • 如何在执行某些行后仅在断点处停止?(How to only stop at a breakpoint after some line was executed?)
  • 以未定义的元素在JSON中循环(loop in JSON with undefined elements)