Wordpress HELP Please

I can’t seem to get my wordpress json to work. I’m brand new to the angularjs and ionic world, so I’ve been reading and watching tutorials. I’ve hit a brick wall.

Basically, I have the usual app.js, then in various other controllers (separate javascripts) I have at the top: angular.module(‘myappname’)

I am able to grab data from reddit through a controller, but not wordpress. I found a good demo template but cannot figure out how to ‘rewrite’ the beginning of the js controller. It is:

angular.module(‘starter.controllers’, [])

.controller(‘AppCtrl’, function($scope, $timeout, $rootScope) {

// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//$scope.$on(’$ionicView.enter’, function(e) {
//});

// Enter your site url here, leaving the /wp-json/wp/v2 part. You must have the WP-API v2 plugin activated on this site
$rootScope.url = ‘http://scottbolinger.com/wp-json/wp/v2/’;

})

Since “AppCtrl” is the main ng-app for that demo template and I’m not using it, what do I need to do to change it to fit my app?

This controller does only one thing: it sets up a global value in root scope for the url fo the wordpress api endpoint.

$rootScope.url = 'http://scottbolinger.com/wp-json/wp/v2/';

You could put it in almost any place that has access to $rootScope.

The most important part is missing here, you must search where $rootScope.url is used, it’s probably in a service.

‘mainappname’ is established in app.js by saying:

(function() {

var app = angular.module(‘myreddit’, [‘ionic’, ‘angularMoment’,‘LocalStorageModule’]);


In my Wordpress controller (WPCtrl.js), it’s below. I don’t see what I’m missing?

angular.module(‘mainappname’)

.controller(‘WordCtrl’, function($scope, $timeout, $rootScope) {

// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//$scope.$on(’$ionicView.enter’, function(e) {
//});

// Enter your site url here, leaving the /wp-json/wp/v2 part. You must have the WP-API v2 plugin activated on this site
$rootScope.url = ‘http://scottbolinger.com/wp-json/wp/v2/’;

})

.controller(‘PostsCtrl’, function( $scope, $http, DataLoader, $timeout, $ionicSlideBoxDelegate, $rootScope ) {

console.log('PostsCtrl');

$scope.loadPosts = function() {

  DataLoader.get( $rootScope.url + 'posts' ).then(function(response) {
    $scope.posts = response.data;
    console.log( response.data );
  }, function(response) {
    console.log('error', response);
  });

}

// Load posts on page load
$scope.loadPosts();

paged = 2;
$scope.moreItems = true;

// Load more (infinite scroll)
$scope.loadMore = function() {

  if( !$scope.moreItems ) {
    return;
  }

  var pg = paged++;

  $timeout(function() {

    DataLoader.get( $rootScope.url + 'posts' + '?page=' + pg ).then(function(response) {

      angular.forEach( response.data, function( value, key ) {
        $scope.posts.push(value);
      });

      if( response.data.length <= 0 ) {
        $scope.moreItems = false;
      }
    }, function(response) {
      $scope.moreItems = false;
      console.log('error');
    });

    $scope.$broadcast('scroll.infiniteScrollComplete');
    $scope.$broadcast('scroll.resize');

  }, 1000);

}

$scope.moreDataExists = function() {
  return $scope.moreItems;
}

// Pull to refresh
$scope.doRefresh = function() {

  console.log('Refreshing!');
  $timeout( function() {

    $scope.loadPosts();

    //Stop the ion-refresher from spinning
    $scope.$broadcast('scroll.refreshComplete');
  
  }, 1000);
    
};

})

.controller(‘PostCtrl’, function($scope, $stateParams, DataLoader, $ionicLoading, $rootScope, $sce ) {

$ionicLoading.show({
noBackdrop: true
});

var singlePostApi = $rootScope.url + ‘posts/’ + $stateParams.postId;

DataLoader.get( singlePostApi ).then(function(response) {
$scope.post = response.data;
// Don’t strip post html
$scope.content = $sce.trustAsHtml(response.data.content.rendered);
$ionicLoading.hide();
}, function(response) {
console.log(‘error’, response);
});

});

Well using $rootScope to store a data access url is not a good practice but it should work.
It could be replaced by an angular constant and by using it in a service.you would name PostService.

You did not say what was the error you encounter.

It’s just not loading anything from wordpress. The spinner just constantly moves without anything ever coming through.

And what do you see in console?

http://scottbolinger.com/wp-json/wp/v2/posts works when requested manually from browser.
Am I using the correct URL or was it just an example?

If I load that template up (templates/posts.html) it says:

Refused to load the script ‘http://localhost:35729/livereload.js?snipver=1’ because it violates the following Content Security Policy directive: “script-src ‘self’ ‘unsafe-inline’ ‘unsafe-eval’”.

Error: [$injector:unpr] Unknown provider: DataLoaderProvider <- DataLoader <- PostsCtrl
http://errors.angularjs.org/1.4.3/$injector/unpr?p0=DataLoaderProvider%20<-%20DataLoader%20<-%20PostsCtrl
at ionic.bundle.js:8900
at ionic.bundle.js:13094
at Object.getService [as get] (ionic.bundle.js:13241)
at ionic.bundle.js:13099
at getService (ionic.bundle.js:13241)
at invoke (ionic.bundle.js:13273)
at Object.instantiate (ionic.bundle.js:13290)
at ionic.bundle.js:17846
at self.appendViewElement (ionic.bundle.js:52338)
at Object.switcher.render (ionic.bundle.js:50456)

OK you have 2 problems:

  1. You must fix the Content Security Policy meta tag in your index.html which prevents your app from using livereload. Search the forum for whitelist plugin.

  2. DataLoader service or factory cannot be injected, probably because you did not include its script in your index.html

The most important thing is to fix #2, then #1 may block you also for WP access

By the way, you’re using an old version of angular: 1.4.3 which will cause you trouble if you wants to run on iOS 9.
You should upgrade angular and /or ionic.

Sorry, but I don’t completely understand what you mean by number 2. I have the javascript files being called in my index and I do have the whitelist plugin on my root/plugins

DataLoader is defined in which script?

Sorry, if you mean my datacontroller for wordpress, it’s defined in WPCtrl.js but what controller to use it via state provider is done in app.js

You mean that you wrote DataLoader service? Then show the definition. please.

For instance in this example below DaatLoader is a factory defined in services.js, if this app did not include services.js then it will fail like yours.

Sorry, but maybe I am missing something about that.

I just downloaded this wordpress API template and really copied the posts.html, and its controller.js which had their wordpress controller. I copied that and saved it was WPCtrl.js and removed angular.module(‘starter.controllers’, []) at the top, and replaced it with angular.module(‘mybodyapp’)

Since their app worked, and I changed what I needed, I don’t see why it’s not. While I’m copying and pasting, I’m also reading on how to do angularjs/ionic so I’m not just using shortcuts.

Ah, it does have services.js

angular.module(‘starter.services’, [])

.factory(‘DataLoader’, function( $http ) {

return {
get: function(url) {
// Simple index lookup
return $http({
url: url,
method: ‘GET’
});
},

}

});

So will it work if I simply replace that angular.module(‘starter.services’,[]) with my angular.module(‘mybodyapp’)

It now says

Refused to load the script ‘http://localhost:35729/livereload.js?snipver=1’ because it violates the following Content Security Policy directive: “script-src ‘self’ ‘unsafe-inline’ ‘unsafe-eval’”.

SVG’s SMIL animations (, , etc.) are deprecated and will be removed. Please use CSS animations or Web animations instead.
Loading FeedCtrl
’webkitMovementX’ is deprecated. Please use ‘movementX’ instead.
‘webkitMovementY’ is deprecated. Please use ‘movementY’ instead.
PostsCtrl
Failed to load resource: the server responded with a status of 404 (Not Found)
error Object
Failed to load resource: the server responded with a status of 404 (Not Found)
error
Refreshing!
Failed to load resource: the server responded with a status of 404 (Not Found)
error Object

Which should mean it’s going back to your point 1 about the whitelist plugin, correct?

Correct, now your app is calling your WP server but it fails due to incorrect CSP.