Skip to content

Instantly share code, notes, and snippets.

@spsaucier

spsaucier/app.js Secret

Last active August 29, 2015 14:16
Show Gist options
  • Save spsaucier/897927e3481648303e68 to your computer and use it in GitHub Desktop.
Save spsaucier/897927e3481648303e68 to your computer and use it in GitHub Desktop.
FFA_SW 7
.directive("getProp", ['$http', '$filter', function($http, $filter) {
return {
// All we're going to display is the scope's property variable.
template: "{{property}}",
scope: {
// Rather than hard-coding 'name' as in 'person.name', we may need
// to access 'title' in some instances, so we use a variable (prop) instead.
prop: "=",
// This is the swapi.co API URL that we pass to the directive.
url: "="
},
link: function(scope, element, attrs) {
// Make our 'capitalize' filter usable here
var capitalize = $filter('capitalize');
// Make an http request the 'url' variable passed to this directive
$http.get(scope.url, { cache: true }).then(function(result) {
// Get the 'prop' property of our returned data so we can use it in the template.
scope.property = capitalize(result.data[scope.prop]);
}, function(err) {
// If there's an error, just return 'Unknown'
scope.property = "Unknown";
});
}
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment