在AngularFire中,我们如何获得普通javascript中的存储引用?

时间:2016-07-28 11:12:58

标签: javascript firebase angularfire firebase-storage

通过使用普通的Javascript,我可以使用firebase存储,如下面的代码

var config = {
    apiKey: "AIzaSyCJ3RdrcdO_Zph5DKPN-RLmJsR5fBD7S5w",
    authDomain: "myapp-90faa.firebaseapp.com",
    databaseURL: "https://myapp-90faa.firebaseio.com",
    storageBucket: "myapp-90faa.appspot.com",
};

firebase.initializeApp(config);

// file from some event e
var file = e.target.files[0];
var storage = firebase.storage().ref('images/'+file.name);
var task = storage.put(file);

但是如何在Angular fire中获取存储对象?在这里我可以参考一个数据库:

var myApp = angular.module('myApp', ['firebase']);
var myProducts = new Firebase('https://myapp-90faa.firebaseio.com/products');

myApp.controller('ProductsCtrl', ['$scope', '$firebaseArray', function($scope,$firebaseArray) {

1 个答案:

答案 0 :(得分:0)

对于AngularJS,从Angularfire的版本2.3开始就可以了:

app.controller("MyCtrl", ["$scope", "$firebaseStorage",
    function($scope, $firebaseStorage) {
        var storageRef = firebase.storage().ref("images/dog");
        $scope.storage = $firebaseStorage(storageRef);
    }
]);

对于版本1.x之外的Angular,AngularFire2还有一个用于firebase存储的API:

constructor(private fb: FirebaseApp) {
    const storageRef = fb.storage().ref("images/dog")
}