通过使用普通的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) {
对于AngularJS,从Angularfire:的2.3版本开始就可以实现
app.controller("MyCtrl", ["$scope", "$firebaseStorage",
function($scope, $firebaseStorage) {
var storageRef = firebase.storage().ref("images/dog");
$scope.storage = $firebaseStorage(storageRef);
}
]);
对于Angular 1.x版本以外的版本,AngularFire2也有一个用于firebase存储的API:
constructor(private fb: FirebaseApp) {
const storageRef = fb.storage().ref("images/dog")
}