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

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

问题描述

一个很菜鸟上下的问题:

我试图用工厂方法来构建资源对象:

  .factory('杂志',[功能($资源){    VAR URL = document.URL;
    变种urlArray = url.split(/);
    变种organId = urlArray [urlArray.length-1];    返回$资源(的http://本地主机/ CI / API /杂志/:身份证',{
        登录ID:organEntity,
        密码:organCommpassword,
        ID:organId
    });
  }])

因为所有PARAMS是pdefined,organEntity和organCommpassword $ P $标记内定义这个方法很简单。

现在对于不同的资源对象,我需要在参数传递出厂的时候被调用。

我想这资源对象的调用code应该是这样的:

  .controller('ResrouceCtrl',函数($范围,杂志){
      $ scope.magazines = Magazines.query();
});

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

我不知道如果我只是传递参数,我可以得到的参数在出厂?如何指定这样的传递参数​​的工厂方法?

例如,假设现在我无法从URL得到organId了,我需要从我的控制器通过它,如何工厂方法内收到organId?


下面是我的资源JS:

  .factory('MagComments',函数($资源){
    返回$资源(的http://本地主机/ dooleystand / CI / API / magCommenct /:身份证',{
      登录ID:organEntity,
      密码:organCommpassword,
      ID:'@magId使用@语法在参数//传递
    });
  })

下面是我的控制器:

  $ scope.magComments = MagComments.query({马吉德:1});

我试过在参数传递,但它会导致一个错误


解决方案

我想我看到你的问题,你需要使用 @ 语法来定义参数,你会通过这样,也我不知道什么登录ID和密码都在做你似乎没有在任何地方定义他们,他们没有被用来作为URL参数,以便他们是被发送的查询参数?

这是我所看到的迄今为止基于什么我可以建议:

  .factory('MagComments',函数($资源){
    返回$资源(的http://本地主机/ dooleystand / CI / API / magCommenct /:身份证',{
      登录ID:organEntity,
      密码:organCommpassword,
      ID:'@magId
    });
  })

@magId 字符串将告诉资源来替换:ID 与物业马吉德的对象,你把它作为参数。<​​/ p>

我建议你阅读过文档这里(我知道这是一个有点不透明)非常认真,看着接近年底的例子中,这应该有很大的帮助。

A very rookie-ish question:

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
    });
  }])

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();
});

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?

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?


Here is my resource 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
    });
  })

Here is my controller:

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

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

解决方案

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'
    });
  })

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天全站免登陆