AngularJS: mirror Wordpress permalink settings -
i'm posting on cause issue angularjs, not wordpress. i'm building single page app using angularjs (1.6.4) , wp rest api. fine, i'm looking solution mirror permalink settings posts.
i thought extend rest api expose them, inject $http service module's config
app.config(function ($routeprovider, $locationprovider) { $routeprovider.when('/', {templateurl: partials.folder + 'home.php', controller: 'allposts'}); var tags = [ '%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%post_id%', '%postname%', '%category%', '%author%' ]; var urlvars = ''; var inj = angular.injector(['ng']); var $http = inj.get("$http"); $http.get('wp-json/permalink_api/v0/settings').then(function (res) { var settings = res.data; var permalink_settings = settings.permalink_settings; //this array contains permalink settings var arr = settings.permalink_settings.permalink_structure.split('/'); //i'll generate string using array... (var = 0; i<arr.length; i++) { if (arr[i] == "") { arr.splice(i,1); i=i-1; } else { if ((ix = tags.indexof(arr[i]))> -1) { var cleantag = ":" + tags[ix].replace(/%/g, ""); urlvars += '/' + cleantag; } else { urlvars += '/' +arr[i]; } } } }).then(function () { //...and create route based on permalink setting $routeprovider.when(urlvars, {templateurl: partials.folder + 'post.html', controller: 'singlepost'}); }); $locationprovider.html5mode(true); }) this code fine once app loaded, suffers fact $http service not yet ready in phase. happens loads whole app, begins applying config, meaning json data loaded late. worst consequence can't access routes directly browser bar, through app's links.
i suppose using interceptors won't change this. mirror permalink structure seo, want achieve if i'm creating spa, not mention gives full control user classic theme.
any mean achieve this? thank you.
edit .htaccess settings set up; defining route statically works in situation.
i solved wordpress function: prints tag contains config.
function pre_configure_angular () { ?> <script> angular.module('single-page').config(function ($routeprovider, $locationprovider) { var tags = [ '%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%post_id%', '%postname%', '%category%', '%author%' ]; var urlvars = ''; var permalink_structure = '<?php echo get_option('permalink_structure'); ?>'; var arr = permalink_structure.split('/'); (var = 0; i<arr.length; i++) { if (arr[i] == "") { arr.splice(i,1); i=i-1; } else { if ((ix = tags.indexof(arr[i]))> -1) { var cleantag = ":" + tags[ix].replace(/%/g, ""); urlvars += '/' + cleantag; } else { urlvars += '/' +arr[i]; } } } $routeprovider.when('/', {templateurl: partials.folder + 'home.php', controller: 'allposts'}); $routeprovider.when(urlvars, {templateurl: partials.folder + 'post.html', controller: 'singlepost'}); $routeprovider.when('/:page', {templateurl: partials.folder + 'page.html', controller: ''}); $locationprovider.html5mode(true); }); </script> <?php } add_action ('wp_head', 'pre_configure_angular'); this allowed me avoid http request server well.
Comments
Post a Comment