Created
May 29, 2015 15:24
-
-
Save adickson311/dd0cfd873c0343a42299 to your computer and use it in GitHub Desktop.
Angular, quick booleans in templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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