Skip to content

Instantly share code, notes, and snippets.

@yoren
Last active Aug 29, 2015
Embed
What would you like to do?
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