简体   繁体   English

如何连接到Firebase

[英]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>
  1. Create a new file(just to keep things clean), call this firebase-config.js 创建一个新文件(只是为了保持干净),将其命名为firebase-config.js
  2. 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.

相关问题 如何将 Firebase 与 Wix 连接 - How to connect Firebase with Wix 如何连接到 Firebase Firestore? - How to connect to Firebase Firestore? 如何将 Firebase 连接到 Node.JS 项目? - How to connect Firebase to a Node.JS project? 如何将 firebase firestore 与我的本地 firebase 仿真器套件连接? - How to connect firebase firestore with my local firebase emulator suite? Firebase/javascript -- 如何将在 firestore 中创建的数据与用户连接 - Firebase/javascript -- How to connect data created in firestore with user 如何使用firebase admin和express连接后端到前端 - How to connect back end to front end with firebase admin and express 如何将 Firebase DB 连接到 Python 中的 Django 项目 - How can I connect Firebase DB to Django project in Python 如何将javascript / angular.js游戏板连接到Firebase数据库 - How to connect javascript/angular.js game board to firebase database Vue-Firebase:如何从同一应用程序连接到2个Firebase - Vue-Firebase: How to connect to 2 firebases from the same app 如何使用带有Firebase的ElasticSearch解决``错误:连接ECONNREFUSED 127.0.0.1:9200&#39;&#39;? - How to resolve 'Error: connect ECONNREFUSED 127.0.0.1:9200' using ElasticSearch with Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM