2013-05-28 136 views
9

我想TE一個變量傳遞給工廠礦山的,但即時通訊不太清楚如何做到這一點,這裏是我的代碼:傳遞變量工廠angularjs

var app = angular.module('docFinder', []); 

app.factory('docFactory', function($http) { 
    var url = 'http://test.example.com?queryString=searchv2&page='; 
    url=url+page; 
    var docFactory = { 
    async: function() { 
     var promise = $http.get(url).then(function (response) { 
     return response.data; 
     }); 
     return promise; 
    } 
    }; 
    return docFactory; 
}); 

app.controller('docTable', function(docFactory, $scope, $filter) { 

    docFactory.async().then(function(d) { 
     $scope.providers = d;  
     init(); 
    }); 

}

我會想從我的控制器通過您async功能在你的因素髮送的頁面到我廠,以便它可以返回我的新查詢

感謝

回答

13

可以傳遞的價值Y:

var docFactory = { 
    async: function(theVarThatIWantedToPass) { 
     var url=// stuff 
     url += theVarThatIWantedToPass; 
    } 
} 

叫正常: docFactory.async(頁)

-1

那是因爲我想有一個工廠來生成和打開PDF文件和許多控制器每過自己的網址和文件名,以保持控制器的輕薄。

這是NIC raboy教程https://blog.nraboy.com/2014/09/manage-files-in-android-and-ios-using-ionicframework/工廠使用的文件傳輸和inappbrowser科爾多瓦插件:

.factory('pdf-service', function($scope, $ionicLoading){ 

if(window.cordova && window.cordova.InAppBrowser){ 
    window.open = window.cordova.InAppBrowser.open; 
    console.log("InAppBrowser available"); 
    } else { 
    console.log("InAppBrowser not available"); 
    } 

    $scope.download = function() { 
    $ionicLoading.show({ 
     template: 'Loading...' 
    }); 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) { 
     fs.root.getDirectory("ExampleProject",{create: true}, 
      function(dirEntry) { 
       dirEntry.getFile(
        "pdf-number-1.pdf", 
        { 
         create: true, 
         exclusive: false 
        }, 
        function gotFileEntry(fe) { 
         var p = fe.toURL(); 
         fe.remove(); 
         ft = new FileTransfer(); 
         ft.download(
          encodeURI("http://www.someservice.com"), 
          p, 
          function(entry) { 
           $ionicLoading.hide(); 
           $scope.imgFile = entry.toURL(); 
          }, 
          function(error) { 
           $ionicLoading.hide(); 
           alert("Download Error Source -> " + error.source); 
          }, 
          false, 
          null 
         );       
        }, 
        function() { 
         $ionicLoading.hide(); 
         console.log("Get file failed"); 
        } 
       ); 
      } 
     ); 
    }, 
    function() { 
     $ionicLoading.hide(); 
     console.log("Request for filesystem failed"); 
    }); 
    } 

    $scope.load = function() { 
     $ionicLoading.show({ 
     template: 'Loading...' 
     }); 

     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) { 
      fs.root.getDirectory(
       "ExampleProject", 
       { 
        create: false 
       }, 
       function(dirEntry) { 
        dirEntry.getFile(
         "pdf-number-1.pdf", 
         { 
          create: false, 
          exclusive: false 
         }, 
         function gotFileEntry(fe) { 
          $ionicLoading.hide(); 
          $scope.imgFile = fe.toURL(); 
          alert(fe.toURL()); 
          window.open(fe.toURL(), '_system', 'location=no,toolbar=yes,closebuttoncaption=Close PDF,enableViewportScale=yes'); 
         }, 
         function(error) { 
          $ionicLoading.hide(); 
          console.log("Error getting file"); 
         } 
       ); 
       } 
     ); 
     }, 
     function() { 
      $ionicLoading.hide(); 
      console.log("Error requesting filesystem"); 
     }); 
    } 
});