如何传递一个控制器变量作为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

相关问答

更多

在ASP.NET MVC 5中将整个对象从视图传递到控制器(Pass entire object from view to controller in ASP.NET MVC 5)

你的物体可能非常大! 查询字符串对您可以通过基于浏览器的数据传递的数据量有限制。 你应该考虑传递一个唯一的id值(记录),并使用它在你的action方法中从db获取整个记录并将其传递给视图。 @foreach(var item in SomeCollection) { <tr> <td> @Html.Action("Update me!", "Update", new { id = item.Id }) </td> </tr> } 并在行动方法 public ActionRes...

我可以将匿名类型传递给我的ASP.NET MVC视图吗?(Can I pass an anonymous type to my ASP.NET MVC view?)

你可以把它传递给视图吗? 是的,但您的观点将不会被强制输入。 但是帮助者会工作。 例如: public ActionResult Foo() { return View(new {Something="Hey, it worked!"}); } //Using a normal ViewPage <%= Html.TextBox("Something") %> 该文本框应该呈现“嘿,它的工作! 作为价值。 那么你可以定义一个视图,其中T被从控制器传递给它的是什么定义的? 好的,但是在编译...

如何在ASP.NET MVC中制作更新面板(How to make update panel in ASP.NET MVC)

您可以在ASP.NET MVC中使用部分视图来获取类似的行为。 部分视图仍然可以在服务器上构建HTML,您只需要将HTML插入适当的位置(实际上,如果您愿意包含MSFT Ajax库,则MVC Ajax帮助者可以为您设置此选项)。 在主视图中,您可以使用Ajax.Begin窗体来设置异步请求。 <% using (Ajax.BeginForm("Index", "Movie", new AjaxOptions { ...

在asp.net mvc中创建“可插入”应用程序(Creating “pluggable” apps in asp.net mvc)

已经在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 ...

ASP.Net实体框架(ASP.Net Entity Framework)

对于任何感兴趣的人,我使用以下代码解决了这个问题 UnityGamersEntities db2 = new UnityGamersEntities(); ObjectQuery<GameCalendar> gc = db2.GameCalendar.Include("GameTitles"); 似乎真的缺乏实体框架的教程,除非你只想使用单个表,我发现很难找到我需要的信息。 希望这将在未来几个月内改变。 for anyone interested I solved...

Asp.net MVC VS ASP.net WebForms?(Asp.net MVC VS ASP.net WebForms?)

实际上,我将ASP.NET MVC看作下一代,因为它是一种进化 - 试图成为更好的编程环境,因为针对Web应用程序的软件开发要求更多的可测试性。 这是一个巨大的野兽。 根据功能决定是否需要它。 MVC文档较少,而且由于采用了较少的RAD方法,因此很难掌握,但似乎一旦进入,这将是一次相当好的体验。 如果你有一个web应用程序(如stackoverflow.com),那么它可能是一个好方法。 DevExpress组件 - 玩得开心...把它们扔掉。 像大多数ASP.NET组件一样,它们不能工作或只能部...

asp.net MVC框架的有用性,而不是使用常规asp.net编写MVC样式?(Usefulness of asp.net MVC framework as opposed to coding MVC style with regular asp.net?)

您将发现使用MVC而非WebForms的许多差异和原因,但这取决于这些差异对您或您的项目是否重要。 路由绝对是MVC的一大优势。 虽然您可以在WebForms中实现自定义路由配置,但它的直观性却大大降低。 另一大优势是单元测试。 与IoC容器和模拟框架一起,MVC使单元测试变得简单。 使用MVC隔离操作和行为并且具体地测试它们要容易得多。 第三个优点是MVC将有助于减少你要编写的意大利面条代码。 如果您不打算使用任何用户控件,那么在您错过MVC中的HTML帮助程序之前不久。 Html,Url,Vi...

ASP.NET MVC - 查看模型模式(ASP.NET MVC - View Model pattern)

你在这里列举了三件事: 网站统计 动态菜单 无论其他什么[...] 可能来自数据库 第三点是陷阱; 仅因为某些内容存在于您的数据库中, 并不意味着它是您的域模型的一部分。 导航可能基于数据库。 它也可能位于站点地图中或硬编码到应用程序中。 它实际上不是应用程序数据 ,而是应用程序配置 。 如果你将它存储在应用程序数据库中,那就没关系(好吧,不是真的),但它不是模型本身的一部分。 同样,站点统计信息通常存储在数据库中,但它们存储在不同的数据库中,特别是分析数据库(许多人只是将其“外包”给Google...

asp.net mvc日志框架(asp.net mvc logging framework)

我个人喜欢让不同的系统记录不同类型的数据。 正如我所看到的,每个都是日志记录角色的不同领域。 分析师不应该关心#1异常,这些异常适用于开发人员,开发人员应该关注#2(Google Analytics或Adobe SiteCatalyst)。 我觉得允许不同的系统根据需要进行日志记录并以不同方式分发日志记录信息是合适的,尽管如你所说它有点多。 这个问题应该在Programmers.StackExchange.Com上提出。 Personally I like having different sys...

我们如何在ASP.NET mvc框架中将数据从一个视图传递到另一个视图(How we can pass data from one view to another in ASP.NET mvc framework)

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

相关文章

更多

Becoming a data scientist

Data Week: Becoming a data scientist Data Pointed, ...

[转]So You Want To Be A Producer

pro-du-cer n. 1. Someone from a game publisher who ...

Solr: a custom Search RequestHandler

As you know, I've been playing with Solr lately, tr ...

按钮样式

网页上有很多功能是通过链接方式传递参数,这种功能链接普通样式就是一个超链接退出,如果将超链接的样式变成 ...

Spring Data: a new perspective of data operations

Spring Data: a new perspective of data operations ...

Create a Bootable MicroSD Card

http://gumstix.org/create-a-bootable-microsd-card.h ...

AngularJS资源

AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方 ...

Drupal Forums instead of phpBB or vBulletin: A casestudy

5th Jan, 10 Drupal drupal advanced forum drupa ...

Python内建函数(A)

abs(x) 说明:abs(x)返回x的绝对值,如果参数是复数,则返回复数的模; 参数x:整 ...

HTML 超链接(a标签、锚)

a标签: anchor锚 1.超链接 -&gt; 点击之后跳转页面 格式: 协 ...

最新问答

更多

sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)

否(它不会使它们无法访问),是(您可以在没有停机的情况下运行它)。 sp_updatestats可以在没有停机的情况下针对实时数据库运行。 No (it doesn't make them inaccessible), and Yes (you can run it without downtime). sp_updatestats can be run against a live database without downtime.

如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)

最终,我们选择了使用Spark Framework for Java实现的后端REST API。 这可能不是最强大的,用户反馈一直是个问题。 我们将命令行界面拆分为提交REST调用,并将结果显示给用户。 Ultimately, we chose to go the route of having a backend REST API that was implemented with the Spark Framework for Java. This may not be the most r

AESGCM解密失败的MAC(AESGCM decryption failing with MAC)

您不能将Encoding.UTF8.GetString应用于任意二进制数据。 它只能解码使用UTF-8编码字符串的结果的字节。 .net实现将默默地破坏数据,而不是默认情况下抛出异常。 您应该使用Base64: Convert.FromBase64String和Convert.ToBase64String You can't apply Encoding.UTF8.GetString to arbitrary binary data. It can only decode bytes that

Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)

我希望能看到更多你的Sass代码等,但我的猜测是你需要在所有嵌套行上使用nest行为。 在我看来,基金会在Sass中的行主要是为了在一个层面上使用。 嵌套在另一行中的任何行都应使用nest行为,除非您希望在列上添加额外的填充。 在你的CodePen中,我能够通过向所有行添加一类collapse来修复列上填充的问题,我认为这与执行$behavior: nest相同$behavior: nest在Sass中$behavior: nest :

湖北京山哪里有修平板计算机的

京山有个联想的专卖店,那里卖平板电脑,地址在中百前面的十字路口右拐 ,他们应该会提供相关的维修服务。

SimplePie问题(SimplePie Problem)

我怀疑由于内容的性质(包含代码),stackoverflow提要不起作用。 我使用许多feed解析器看似“正常”的feed有类似的问题,尽管我最近运气最多的是Zend_Feed。 试试吧 I suspect the stackoverflow feed is not working due to the nature of the content (contains code). I have had similar issues with seemingly "normal" feeds us

在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)

是的,您可以通过getApplicationContext()任意数量的时间(后台任务), getApplicationContext()仅返回应用程序的context 。 Yes, you can pass getApplicationContext() any number of time (Background Tasks ) you want, getApplicationContext() simply returns context of the application.

HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)

这是我最终做的事情: 我无法以这种方式提供完全访问权限,而是在project level folder中设置了一个虚拟HTML页面,该页面单击自身以重定向到位于separate, non-project util folder的HTML文件。 这允许我保留除了那个之外的所有内容,非常小的文件分开但不存在文件访问问题。 Here is what I ended up doing: I wasn't able to provide full access exactly this way, but

为什么我会收到链接错误?(Why do I get a linker error?)

看起来您的编译器错误地将名称引入到全局名称空间中,而不是C ++ 11 3.5 / 7中指定的最内层名称空间( Bushman ): 如果没有找到具有链接的实体的块范围声明来引用某个其他声明,那么该实体是最内层封闭名称空间的成员。 代码按照预期在GCC上编译: http : //ideone.com/PR4KVC 你应该能够通过在构造函数的块作用域中声明它之前(或代替它)在正确的名称空间中声明该函数来解决该bug。 但是,我无法访问您的编译器来测试它。 It looks like your co

如何正确定义析构函数(How to properly define destructor)

在C ++中,你需要手动释放内存。 没有垃圾收集器。 您显然需要在析构函数内手动释放内存。 如果您使用new分配内存,则需要对在deconstructor中使用new分配的每个资源使用delete ,例如: class1::~class1(void) { delete resource1; delete resource2; etc... } In C++ you need to free the memory manually. There's no garbage