[英]How to connect to Firebase
I am trying to connect to Firebase using AngularJS. 我正在尝试使用AngularJS连接到Firebase。
I know I have to set config info etc. But I don't know where and how. 我知道我必须设置配置信息等。但是我不知道在哪里以及如何。
Where can I put and sync my config in my codes? 我在哪里可以在我的代码中放置并同步我的配置?
Saying my config is like this: 说我的配置是这样的:
firebase.initializeApp(config);
Here are my codes : 这是我的代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test1 with Angular1</title>
<!-- Angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.2.4/firebase.js"></script>
<!-- AngularFire -->
<script src="https://cdn.firebase.com/libs/angularfire/1.2.0/angularfire.min.js"></script>
<script>
var app = angular.module("app",["firebase"]);
app.controller("myController",["$scope","$firebaseObject",function($scope,$firebaseObject){
var ref = new Firebase(<url>);
}]);
</script>
</head>
<body>
<div class="container" ng-app="app" ng-controller="myController">
{{ 1+1 }}
</div>
</body>
</html>
Add your config to the file 将您的配置添加到文件
var config = { apiKey: "AIzaSyBrPs1GLV-0APAs_3PFpvldGbcgE-eVYY", authDomain: "letsayone.firebaseapp.com", databaseURL: "https://letsayone.firebaseio.com", storageBucket: "letsayone.appspot.com", messagingSenderId: "175605985282" }; firebase.initializeApp(config);
3 Now add the js file to your index.html, before your controller codes. 3现在,在控制器代码之前,将js文件添加到index.html。
....
<script src="https://cdn.firebase.com/libs/angularfire/1.2.0/angularfire.min.js"></script>
<script src="firebase-config.js"></script>
<script>...your controller codes here</script>
Note you should be using the latest version of firebase. 请注意,您应该使用最新版本的Firebase。 3.xx Firebase documentation
3.xx Firebase文档
Something really confuses me about your question, you are using the 2.0 SDK, and at the same time config files which I think is a 3.0 feature? 关于您的问题,确实让我感到困惑的是,您使用的是2.0 SDK,同时我认为这是3.0功能的配置文件? Correct me if Im wrong!
如果我错了纠正我!
First of all, try to stay away from the new operator. 首先,尝试远离新的操作员。 Its not nice for unit testing.
它对单元测试不好。
Its a better way to use constants and angular services. 这是使用常量和角度服务的更好方法。
app
.constant('firebaseUrl', "https://letsayone.firebaseio.com")
.service('rootRef', ['firebaseUrl', Firebase])
In your controller, you want to inject rootRef 在您的控制器中,您想注入rootRef
app.
controller('mainCtrl', ['$scope', 'rootRef', function($scope, rootRef){
}])
Also don't forget to inject firebaseObject or firebaseArray depending on the one you are going to use! 另外,不要忘记根据您要使用的对象注入firebaseObject或firebaseArray!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.