Yes, I think refactoring is wise. Your example is simple, but it’s easy to imagine that you have many conditions. I did something like this when many elements have complex display conditions:
$scope.render = {
foobar: function() {
return $scope.status == 'state1' || $scope.status == 'state2'
},
baz: function() {
return $scope.status == 'state3'
}
}
Then use in view:
<span ng-if="render.foobar()">Foobar</span>
<span ng-if="render.baz()">Baz</span>
Demo: http://plnkr.co/lv4w9dLN8oN0bBF23VKp
This saves a logical trace in a small representation and makes it easy to reuse logic for multiple elements.
source
share