How to pass in parameters when use resource service?

前端 未结 2 433
刺人心
刺人心 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: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();
    
           ....
    
    }
    

提交回复
热议问题