Last active
August 29, 2015 14:21
-
-
Save yoren/4fcfedcbfa82a1c258c8 to your computer and use it in GitHub Desktop.
Creating A Service in AngularJS WordPress Theme
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
<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> | |
<!-- ... --> |
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
//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; | |
//.. | |
}]); |
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
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]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original Post: Improving Your AngularJS WordPress Theme With A Service