I'm fairly new to angular so apologies if this is a noob question. Within my small Ionic/Angular app I have a situation where I need to display only 1 of 3 buttons depending on some basic logic. Currently I'm going this in my controller:
if ($scope.book.borrowedBy == $rootScope.signedInUser.email) {
$scope.mode = 'return';
} else if ($scope.book.borrowedBy) {
$scope.mode = 'request';
} else {
$scope.mode = 'borrow';
}
$scope.borrow = function() {
$scope.book.borrowedBy = $rootScope.signedInUser.email;
$scope.book.$save();
$scope.mode = 'return';
}
$scope.return = function() {
$scope.book.borrowedBy = null;
$scope.book.$save();
$scope.mode = 'borrow';
}
and this in my view partial:
<a href="mailto:{{book.borrowedBy}}" ng-show="mode === 'request'" class="button button-block button-positive">
Request book
</a>
<button ng-show="mode === 'return'" class="button button-block button-positive" ng-click="return()">
Return book
</button>
<button ng-show="mode === 'borrow'" class="button button-block button-positive" ng-click="borrow()">
Borrow book
</button>
Whilst this works it feels messy as hell. According to best practices, should this be in a directive? Or perhaps I should use ng-if
?
Is it possible to output different template code based on conditionals from within a directive (I've only created very basic ones so far)?
If it should be a directive, would someone be able to give me an example of similar behaviour as a starting point?
Thanks in advance