HTML file:
<div class="header" ng-controller='HomeCtrl as homeCtrl'>
<div class="panel-header container">
<h1>UkrBook</h1>
<ul class="nav nav-tabs navbar-right">
<li ng-class="{active: homeCtrl.isSelected(1)}"><a href ng-click="homeCtrl.selectTab(1, 'home')">Головна</a></li>
<li ng-hide="" ng-class="{active: homeCtrl.isSelected(2)}"><a href ng-click="homeCtrl.selectTab(2, 'login')">Увійдіть</a></li>
<li ng-class="{active: homeCtrl.isSelected(3)}"><a href ng-click="homeCtrl.selectTab(3, 'register')">Реєстрація</a></li>
<li ng-class="{active: homeCtrl.isSelected(4)}"><a href ng-click="homeCtrl.selectTab(4, 'about')">Про нас</a></li>
<li ng-class="{active: homeCtrl.isSelected(5)}"><a href ng-click="homeCtrl.selectTab(5, 'profile')">Профіль</a></li>
<li ng-class="{active: homeCtrl.isSelected(6)}"><a href ng-click="homeCtrl.selectTab(6, 'home', Auth.logout())">Вийти</a></li>
</ul>
</div>
</div>
Controller piece:
.controller('HomeCtrl', function($state, Auth) {
var homeCtrl = this;
homeCtrl.tab = 1;
homeCtrl.logout = Auth.logout;
homeCtrl.selectTab = function(setTab, state, callback) {
homeCtrl.tab = setTab;
$state.go(state).then(callback),
function(error) {
console.log(error);
};
};
Auth service:
.factory('Auth', function($firebaseAuth, FirebaseUrl, $state) {
var ref = new Firebase(FirebaseUrl);
var Auth = $firebaseAuth(ref);
Auth.logout = function(){
ref.unauth().then(function(){
console.log('Logged out');
$state.go('home');
}, function(error){
authCtrl.error = error;
});
};
return Auth;
The problem is following: Auth.logout method from HTML file can't be loaded, though I'm using HomeCtrl as a controller, which has DI Auth factory.
So, to make it clear, this one won't work:
ng-click="homeCtrl.selectTab(6, 'home', Auth.logout())"
I have to add homeCtrl.logout = Auth.logout; to the HomeCtrl and call homeCtrl.logout in my HTML file.
Could someone please explain to me, why can't I do so and is it a proper way of injecting services? Also I would appreciate some good articles with examples about this. Thanks.