如何在使用资源服务时传递参数?

时间:2021-05-28 21:20:29

A very rookie-ish question:

一个非常rookie-ish的问题:

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.

这种方法很简单,因为所有params都是预定义的,在标签内部定义了organEntity和organCommpassword。

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

我知道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?

我想知道如果我只是传递参数,我能在工厂得到参数吗?如何在factory方法中指定这些传入的参数?

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?

例如,现在假设我不能再从url获取风琴导航,我需要从我的控制器中传递它,如何在factory方法中接收风琴导航?


Here is my resource js:

以下是我的资源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

我试图传入参数,但它会导致错误

2 个解决方案

#1


47  

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?

我想我看到你的问题,你需要使用@语法来定义参数将通过这种方式,我也不敢肯定loginID或密码在做什么你似乎不定义任何地方,他们不是被用作URL参数也被作为查询参数发送吗?

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.

@magId字符串将告诉资源用属性magId替换:id作为参数传递给对象。

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.

我建议仔细阅读这里的文档(我知道它有点不透明),并在最后查看示例,这应该会有很大帮助。

#2


8  

I suggest you to use provider. Provide is good when you want to configure it first before to use (against Service/Factory)

我建议你使用提供商。当您想要在使用之前配置它(针对服务/工厂)时,提供是好的。

Something like:

喜欢的东西:

.provider('Magazines', function() {

    this.url = '/';
    this.urlArray = '/';
    this.organId = 'Default';

    this.$get = function() {
        var url = this.url;
        var urlArray = this.urlArray;
        var organId = this.organId;

        return {
            invoke: function() {
                return ......
            }
        }
    };

    this.setUrl  = function(url) {
        this.url = url;
    };

   this.setUrlArray  = function(urlArray) {
        this.urlArray = urlArray;
    };

    this.setOrganId  = function(organId) {
        this.organId = organId;
    };
});

.config(function(MagazinesProvider){
    MagazinesProvider.setUrl('...');
    MagazinesProvider.setUrlArray('...');
    MagazinesProvider.setOrganId('...');
});

And now controller:

现在控制器:

function MyCtrl($scope, Magazines) {        

        Magazines.invoke();

       ....

}

#1


47  

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?

我想我看到你的问题,你需要使用@语法来定义参数将通过这种方式,我也不敢肯定loginID或密码在做什么你似乎不定义任何地方,他们不是被用作URL参数也被作为查询参数发送吗?

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.

@magId字符串将告诉资源用属性magId替换:id作为参数传递给对象。

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.

我建议仔细阅读这里的文档(我知道它有点不透明),并在最后查看示例,这应该会有很大帮助。

#2


8  

I suggest you to use provider. Provide is good when you want to configure it first before to use (against Service/Factory)

我建议你使用提供商。当您想要在使用之前配置它(针对服务/工厂)时,提供是好的。

Something like:

喜欢的东西:

.provider('Magazines', function() {

    this.url = '/';
    this.urlArray = '/';
    this.organId = 'Default';

    this.$get = function() {
        var url = this.url;
        var urlArray = this.urlArray;
        var organId = this.organId;

        return {
            invoke: function() {
                return ......
            }
        }
    };

    this.setUrl  = function(url) {
        this.url = url;
    };

   this.setUrlArray  = function(urlArray) {
        this.urlArray = urlArray;
    };

    this.setOrganId  = function(organId) {
        this.organId = organId;
    };
});

.config(function(MagazinesProvider){
    MagazinesProvider.setUrl('...');
    MagazinesProvider.setUrlArray('...');
    MagazinesProvider.setOrganId('...');
});

And now controller:

现在控制器:

function MyCtrl($scope, Magazines) {        

        Magazines.invoke();

       ....

}