Skip to content

Instantly share code, notes, and snippets.

@amielucha
Created October 10, 2015 16:16
Show Gist options
  • Save amielucha/56e7d09cb7f570296f0b to your computer and use it in GitHub Desktop.
Save amielucha/56e7d09cb7f570296f0b to your computer and use it in GitHub Desktop.
AngularJS ♥ WordPress Tutorial part 9 #tutorial
(function(){
var app = angular.module('angular♥wp', ['ngRoute']);
var api = {};
// JSON content location
api.query = 'http://amielucha.com/wp-json/wp/v2/posts/';
app.config(function($routeProvider, $locationProvider){
// Enable html5 mode
$locationProvider.html5Mode(true);
// configure routing
$routeProvider.when('/', {
templateUrl: 'content.html',
controller: 'Main',
});
});
app.controller('Main', ['$scope', '$http', '$routeParams', '$sce', function($scope, $http, $routeParams, $sce){
$http.get( api.query ).success(function(res){
// iterate through each field to set it as trusted
angular.forEach($scope.posts, function(value, key) {
// trust each title and excerpt in the object
this[key].title.rendered = $sce.trustAsHtml(value.title.rendered);
this[key].excerpt.rendered = $sce.trustAsHtml(value.excerpt.rendered);
}, $scope.posts);
});
}]);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment