9

我想将变量传递给我的工厂,但我不太确定该怎么做,这是我的代码:

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

}

我想将页面从我的控制器发送到我的工厂,以便它可以返回我的新查询

谢谢

4

2 回答 2

13

您可以通过async工厂中的函数传递值:

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

正常调用:docFactory.async(page)

于 2013-05-28T20:39:51.190 回答
-1

那是因为我希望有一个工厂来生成和打开 pdf 文件和许多控制器,每个控制器都传递他自己的 url 和文件名,以保持控制器薄。

这是 nic raboy 教程https://blog.nraboy.com/2014/09/manage-files-in-android-and-ios-using-ionicframework/的工厂,它使用文件传输和 inappbrowser cordova 插件:

.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");
      });
    }
});
于 2015-06-16T17:44:45.390 回答