Skip to content

Instantly share code, notes, and snippets.

@adickson311
Created May 29, 2015 15:24
Show Gist options
  • Save adickson311/dd0cfd873c0343a42299 to your computer and use it in GitHub Desktop.
Save adickson311/dd0cfd873c0343a42299 to your computer and use it in GitHub Desktop.
Angular, quick booleans in templates
//
// Problem: In Angular, you're using directives in the HTML to reference properties on the scope,
// but you end up with super long expressions in your html like ...
//
// ng-if="myCity.publicZoo.myEscapedAnimal.type === animals.mammals.primates.orangutan"
//
// and you end up exposing constants to the scope so you can do those checks
//
// $scope.animals = Animals
//
// I prefer to something like this ng-if="is('orangutan')"
var is = {
'orangutan': function(){
return $scope.myCity.publicZoo.myEscapedAnimal.type === Animals.mammals.primates.orangutan;
}
};
$scope.is = function(key){
return _.result(is, key);
};
// Thoughts?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment