Skip to content

Instantly share code, notes, and snippets.

@yoren
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoren/4fcfedcbfa82a1c258c8 to your computer and use it in GitHub Desktop.
Save yoren/4fcfedcbfa82a1c258c8 to your computer and use it in GitHub Desktop.
Creating A Service in AngularJS WordPress Theme
<search-form></search-form>
<p>Categories:</p>
<ul>
<li ng-repeat="category in data.categories">
<span ng-if="current_category_id && category.ID == current_category_id" ng-bind-html="category.name"></span>
<a ng-if="!current_category_id || category.ID != current_category_id" href="category/{{category.slug}}" ng-bind-html="category.name"></a>
</li>
</ul>
<!-- ... -->
//Main controller
app.controller('Main', ['$scope', '$http', 'WPService', function($scope, $http, WPService) {
WPService.getAllCategories();
$scope.data = WPService;
//...
}]);
//...
//Category controller
app.controller('Category', ['$scope', '$routeParams', '$http', 'WPService', function($scope, $routeParams, $http, WPService) {
WPService.getAllCategories();
$scope.data = WPService;
//...
}]);
//Paged controller
app.controller('Paged', ['$scope', '$routeParams', '$http', 'WPService', function($scope, $routeParams, $http, WPService) {
WPService.getAllCategories();
$scope.data = WPService;
//..
}]);
function WPService($http) {
var WPService = {
categories: []
};
WPService.getAllCategories = function() {
if (WPService.categories.length) {
return;
}
return $http.get('wp-json/taxonomies/category/terms').success(function(res){
WPService.categories = res;
});
};
return WPService;
}
app.factory('WPService', ['$http', WPService]);
@yoren
Copy link
Author

yoren commented May 29, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment