首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Angular js获取Wordpress帖子

如何使用Angular js获取Wordpress帖子
EN

Stack Overflow用户
提问于 2017-02-16 02:16:20
回答 1查看 841关注 0票数 3

我正在尝试使用WordPress rest api插件和$http服务将我的angular应用程序连接到wordpress CM。然而,由于某些原因,数据return.Can没有人提供使用wordpress rest api插件为wordpress检索数据的正确方法吗?

下面我列出了我的代码,其中包括我的html和JavaScript。此外,我还列出了我正在尝试获取测试数据的本地rest api。

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.6.2/angular-sanitize.js"></script>
</head>
<body>
<div class="masthead" np-app="myApp">
                <div class="item">
                    <div class="masthead-content">
                        <div class="copy">
                            <div class="container">
                                <div class="row">
                                    <div class="col-md-6"  ng-controller="Ctrl">
                                    <div ng-repeat="post in posts | limitTo: 1">
                                        <h2 ng-bind-html="post.title.rendered">{{posts}}</h2>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    <script>

var app = angular.module('myApp', ['ngSanitize']);
    app.controller('myCtrl', function($http, $scope) {
        $http.get("http://localhost/wp/wp-json/wp/v2/posts/4").success(function(data) {
        $scope.posts = data;

    });
});

</script>
EN

回答 1

Stack Overflow用户

发布于 2018-06-25 02:33:23

调用WordPress的rest API没有问题

$http.get('https://yourblog.com/wp-json/wp/v2/posts/?categories=1&per_page=2&orderby=id&order=asc&page=1').
    then(function(response) {
        console.log(response);
        $scope.posts = response.data;
    });

此代码在我的AngularJS或ionic应用程序中运行

<div ng-repeat="post in posts">
 <h2>{{post.title}}</h2>
</div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42257132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档