How to pass in parameters when use resource service?

前端 未结 2 425
刺人心
刺人心 2020-12-13 04:18

A very rookie-ish question:

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

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

    var url =         


        
相关标签:
2条回答
  • 2020-12-13 04:40

    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.

    0 讨论(0)
  • 2020-12-13 04:51

    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();
    
           ....
    
    }
    
    0 讨论(0)
提交回复
热议问题