簡體   English   中英

WP-json沒有與angularJS一起顯示

[英]WP-json is not displaying with angularJS

我想用已安裝的插件WP-REST v2顯示我的WordPress帖子,以從槽AngularJS獲取數據,但是有問題。 我無法顯示它並根據本文進行編碼。

https://amielucha.com/angularjs-json-rest-api-with-wordpress

所以我的主要主題是如何使用angularjs做到這一點。

這些是我的代碼:

app.js:

(function() {
var app = angular.module('app', ['ngRoute']),
    api = {};

// JSON content Location
api.query = 'http://myy.website/dlrz/wp-json/wp/v2/posts/';

app.config([ '$routeProvider', '$locationProvider', '$qProvider', function($routeProvider, $locationProvider, $qProvider) {
    // Enable HTML5 Mode
    $locationProvider.html5Mode(true);

    // configure routing
    $routeProvider
        .when('/', {
        templateUrl: myLocal.partials + 'main.html',
        controller: 'Main'
    });

    $qProvider.errorOnUnhandledRejections(false);
}]);

// Set Controller and get JSON-Data
app.controller('Main', [ '$scope', '$http', '$routeParams', '$sce', function($scope, $http, $routeParams, $sce) {
    $http.get(api.query).then(function(res){
        $scope.posts = res;
        // interate through each field to set it as trusted
        angular.forEach(res, 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);
        console.log($scope.posts);
        console.log($sce);

    });
}]);
})();

main.html:

<section>
<article class="well" ng-repeat="post in posts">
    <header>
        <h1 ng-bind-html="post.title.rendered">
            {{post.title.rendered}}
        </h1>
    </header>
    <div class="post-content" ng-bind-html="post.excerpt.rendered">
        {{post.excerpt.rendered}}
    </div>
</article>

index.php:

    <?php /*get_header(); */?>


<!DOCTYPE html>
<html ng-app="app">

    <head>
        <!-- Required meta tags always come first -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <base href="<?php echo site_url();?>/">

        <title>
            <?php bloginfo( 'name'); ?>
        </title>
        <?php wp_head(); ?>
    </head> 
    <body>   
        <h1>Hello World!</h1>

        <div ng-view></div>


        <footer>
            &copy; <?php echo date( 'Y' ); ?> Dart Liga Verband Zürich
        </footer>

    </body>
</html>     
<?php wp_footer(); ?>
<?php/* get_footer(); */?>

這是json文件: http//myy.website/dlrz/wp-json/wp/v2/posts

問題在於如何從響應中獲取數據。 您正在使用

$scope.posts = res;

但這應該是

$scope.posts = res.data;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM