使用资源服务时如何传入参数? [英] How to pass in parameters when use resource service?

查看:21
本文介绍了使用资源服务时如何传入参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常菜鸟的问题:

我正在尝试使用工厂方法构建资源对象:

I'm trying to build resource object using factory method:

.factory('Magazines', [function ($resource) {

    var url = document.URL;
    var urlArray = url.split("/");
    var organId = urlArray[urlArray.length-1];

    return $resource('http://localhost/ci/api/magazines/:id', {
        loginID : organEntity,
        password : organCommpassword,
        id : organId
    });
  }])

这个方法很简单,因为所有的参数都是预定义的,organEntity 和organCommpassword 是在标签中定义的.

This method is easy because all params are predefined, organEntity and organCommpassword are defined inside tag.

现在对于不同的资源对象,我需要在调用工厂时传入参数.

Now for a different resource object, I need to pass in parameter when the factory is called.

我想这个资源对象的调用代码应该是这样的:

I imagine the calling code of this resource object should look like:

.controller('ResrouceCtrl', function($scope, Magazines) {
      $scope.magazines = Magazines.query();
});

我知道 query() 方法可以添加参数:Magazines.query(params, successcb, errorcb);

I know query() method can add parameters: Magazines.query(params, successcb, errorcb);

我想知道如果我只是传入参数,我可以在工厂拿到参数吗?如何在工厂方法中指定这样的传入参数?

I wonder if I just pass in parameters, can I get the parameter at the factory? How to specify such passed in parameters in the factory method?

例如,现在假设我不能再从 url 中获取organId,我需要从我的控制器中传入它,如何在工厂方法中接收organId?

For example, now suppose I cannot get organId from url anymore, I need to pass it in from my controller, how to receive organId within the factory method?

这是我的资源js:

.factory('MagComments', function ($resource) {


    return $resource('http://localhost/dooleystand/ci/api/magCommenct/:id', {
      loginID : organEntity,
      password : organCommpassword,
      id : '@magId' //pass in param using @ syntax
    });
  })

这是我的控制器:

$scope.magComments = MagComments.query({magId : 1});

我尝试传入参数,但导致错误

I tried to pass in the parameter, but it causes an error

推荐答案

我想我看到你的问题了,你需要使用 @ 语法来定义你会以这种方式传递的参数,我也是不确定登录 ID 或密码在做什么,您似乎没有在任何地方定义它们,也没有将它们用作 URL 参数,所以它们是作为查询参数发送的吗?

I think I see your problem, you need to use the @ syntax to define parameters you will pass in this way, also I'm not sure what loginID or password are doing you don't seem to define them anywhere and they are not being used as URL parameters so are they being sent as query parameters?

根据我目前所见,我可以提出以下建议:

This is what I can suggest based on what I see so far:

.factory('MagComments', function ($resource) {
    return $resource('http://localhost/dooleystand/ci/api/magCommenct/:id', {
      loginID : organEntity,
      password : organCommpassword,
      id : '@magId'
    });
  })

@magId 字符串将告诉资源将 :id 替换为您作为参数传递的对象上的属性 magId.

The @magId string will tell the resource to replace :id with the property magId on the object you pass it as parameters.

我建议非常仔细地阅读此处的文档(我知道它有点不透明)并且看看最后的例子,这应该会有很大帮助.

I'd suggest reading over the documentation here (I know it's a bit opaque) very carefully and looking at the examples towards the end, this should help a lot.

这篇关于使用资源服务时如何传入参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆