Now, go to Database under Develop section on the left-side. Click Get Started under Realtime Database.
Enable Start in test mode. To keep things simple for this tutorial, we are going to use test mode. It makes the database open to read and write publicly. Don’t worry, you can change these settings later on.
<!DOCTYPE html> <html ng-app="app"><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"><title page-title></title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css" /><style>
body{
font-family:”Open Sans” , sans-serif
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Address Book</a>
</div>
<ul class="nav navbar-nav">
<li><a ui-sref="home">Home</a></li>
<li><a ui-sref="add">Add</a></li>
</ul>
</div>
</nav>
<div ui-view></div><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script src="//unpkg.com/@uirouter/angularjs/release/angular-ui-router.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/2.3.0/angularfire.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-datatables/0.6.2/angular-datatables.min.js"></script><script>
// Initialize Firebase
var config = {
apiKey: ********************,
authDomain: ********************,
databaseURL:********************,
projectId: **********************,
storageBucket: **********************,
messagingSenderId: **********************,
};
firebase.initializeApp(config);
</script>
<script src="scripts/app.js"></script>
<script src="scripts/config.js"></script>
<script src="scripts/controllers.js"></script>
</body>
</html>
Step 2: Create a scripts folder and create following files:
app.js
angular.module('app', [
'ui.router',
'firebase',
'datatables'
])
Here we need to import modules of Angular UI Router, AngularFire and datatables (to be explained later).
config.js
function config($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', {
url: "/home",
templateUrl: "views/home.html"
})
.state('add', {
url: "/add",
templateUrl: "views/add.html"
})
}
angular
.module('app')
.config(config)
Step 3: Create a views folder and create home.html file with just “Hello World!” text.
Create add.html page as:
<div class="container" ng-controller="addContactCtrl">
<form ng-submit="add()">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" ng-model="formData.name">
</div>
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" ng-model="formData.address">
</div>
<div class="form-group">
<label>Phone No.</label>
<input type="text" class="form-control" ng-model="formData.phone">
</div>
<button class='btn btn-default' type="submit">Save</button>
</form>
</div>
Here the input text is stored in the formData object using ng-model. When the user submits the form, add function is executed.
Add function will store the new person’s details in Firebase.
Step 4: Create controllers.js and add the following code:
function addContactCtrl($scope, $state, $firebaseObject) {
let ref = firebase.database().ref('Contacts');
let pushKey = ref.push().key;
$scope.formData = $firebaseObject(ref.child(pushKey));
$scope.add = function(){
$scope.formData.$save().then(() => {
$state.go('home');
});
}
};
angular
.module('app')
.controller('addContactCtrl', addContactCtrl)
Add page now looks like this:
Let’s explain it step by step:
function addContactCtrl($scope, $state, $firebaseObject) {
wlet ref = firebase.database().ref('Contacts');
let pushKey = ref.push().key;
$scope.formData = $firebaseObject(ref.child(pushKey));
$scope.add = function(){
$scope.formData.$save().then(() => {
$state.go('home');
});
}
function homeCtrl($scope, $firebaseObject){
const ref = firebase.database().ref('Contacts');
$scope.contacts = $firebaseObject(ref);
}
angular
.module('app')
.controller('homeCtrl', homeCtrl)
.controller('addContactCtrl', addContactCtrl)
Here we created a reference to the Contacts key in Firebase and downloaded its data in $scope.contacts using $firebaseObject.
Step 2: Create home.html in views folder
<div class='container' ng-controller="homeCtrl">
<table datatable="ng" class="table row-border table-striped table-hover compact display">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone No.</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in contacts">
<td>{{contact.name}}</td>
<td>{{contact.address}}</td>
<td>{{contact.phone}}</td>
</tr>
</tbody>
</table>
</div>
Here we have initialised datatables using the datatables=”ng” directive. It specifies that the data in the table is rendered using AngularJS.
In tbody, ng-repeat directive creates rows for every contact in $scope.contacts object.
And thus, the basic contacts app is now complete. You are free to improve it by adding more features like editing or deletion of contacts.