javascript - 如何在 AngularJS 部分文件中使用 php (wordpress) 函数?

标签 javascript php angularjs json wordpress

我正在使用 angularjs 加载我的 wordpress 帖子列表,但是我无法让我的任何 php 函数与我的部分文件一起工作。

我试过使用诸如 search.php 之类的东西而不是 search.html,但是在这样做时我得到了诸如 fatal error get_post_meta is undefined 之类的错误。

现在我知道我们不应该将客户端和服务器端混合在一起,我可能会使用某种服务来解析我的 php,但我不知道该怎么做。我需要我的 search.php 来呈现我的 php 标签,以便我可以显示自定义字段,并使用我在那里的几个 php 函数。

最好的方法是什么?

在我的页面模板 (.php) 上我有 --

<div id="page" ng-app="app">
        <header>
            <h1>
                <a href="<?php echo home_url(); ?>">Search</a>
            </h1>
        </header>
  <?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

   <div ng-Cloak ng-controller="MyController" class="my-controller">
<div ng-view></div>     
</div>
  <?php endwhile; ?>
 <?php endif; ?>
<?php rewind_posts(); ?>
<div ng-controller="OtherController" class="other-controller">
          <div class="text-center">
          <dir-pagination-controls boundary-links="true" on-page-change="pageChangeHandler(newPageNumber)" template-url="/partials/dirPagination.tpl.html"></dir-pagination-controls>
          </div>
        </div>
        <footer>
            &copy; <?php echo date( 'Y' ); ?>
        </footer>
    </div>

在我想被调入 View 的 php 文件中有如下函数 --

<?php 
$pcomp1b = get_post_meta(get_the_ID(), 'pa_meta_comp1b', true); 
$pcomp1c = get_post_meta(get_the_ID(), 'pa_meta_comp1c', true);
$pcomp1d = get_post_meta(get_the_ID(), 'pa_meta_comp1d', true); ?>

数学 --

if( is_numeric( $price1 ) ) {
    $a1 = $price1;
}
$b1 = $pcomp1d;
$sqft1 = str_replace( ',', '', $b1 );

if( is_numeric( $sqft1 ) ) {
    $b1 = $sqft1;
}

$a2 = $pcomp2f;
$price2 = str_replace( ',', '', $a2 );

if( is_numeric( $price2 ) ) {
    $a2 = $price2;
}
$b2 = $pcomp2d;
$sqft2 = str_replace( ',', '', $b2 );

if( is_numeric( $sqft2 ) ) {
    $b2 = $sqft2;
}

$a3 = $pcomp3f;
$price3 = str_replace( ',', '', $a3 );

if( is_numeric( $price3 ) ) {
    $a3 = $price3;
}
$b3 = $pcomp3d;
$sqft3 = str_replace( ',', '', $b3 );

if( is_numeric( $sqft3 ) ) {
    $b3 = $sqft3;
}
$ppsqft1 = ROUND($price1 / $sqft1);
$ppsqft2 = ROUND($price2 / $sqft2);
$ppsqft3 = ROUND($price3 / $sqft3);

$ppsav = ROUND((($ppsqft1 + $ppsqft2 + $ppsqft3)/3));

$b4 = $property_area;
$parea = str_replace( ',', '', $b4 );

if( is_numeric( $parea ) ) {
    $b4 = $parea;
}
$ehvp = $ppsav * $parea;
$homevalue = number_format($ehvp, 0, '.', ',');
echo '$' . $homevalue; ?>

和函数——

<?php if (class_exists('MRP_Multi_Rating_API')){ MRP_Multi_Rating_API::display_rating_result(array('rating_item_ids' => 2, 'show_count' => false, 'result_type' => 'value_rt', 'no_rating_results_text' => 'N/A'));} ?>

我的应用程序脚本 --

var app = angular.module('app', ['ngRoute', 'ngSanitize', 'angularUtils.directives.dirPagination'])

function MyController($scope) {


$scope.currentPage = 1;

$scope.pageSize = 2;

$scope.posts = [];


$scope.pageChangeHandler = function(num) {

console.log('search page changed to ' + num);
  };
}



function OtherController($scope) {

$scope.pageChangeHandler = function(num) {

console.log('going to page ' + num);
  };
}

app.config(function(paginationTemplateProvider) {
    paginationTemplateProvider.setPath('/partials/dirPagination.tpl.html');
});

app.config(function($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);

    $routeProvider
    .when('/search-results', {
        templateUrl: myLocalized.partials + 'main.html',
        controller: 'Main'
    })
    .when('/:ID', {
        templateUrl: myLocalized.partials + 'content.html',
        controller: 'Content'
    });
})
app.controller('Main', function($scope, $http, $routeParams) {
    $http.get('wp-json/posts?type=property').success(function(res){
        $scope.posts = res;
    });
})
app.controller('Content', function($scope, $http, $routeParams) {
    $http.get('wp-json/posts?type=property/?filter["posts_per_page"]=25&filter["orderby"]=date&filter["order"]=desc/' + $routeParams.ID).success(function(res){
        $scope.post = res;
    });
});


app.controller('MyController', MyController);
app.controller('OtherController', OtherController);

那么我怎样才能让它与 ng-view 和我的部分模板一起工作呢?

更新

我确实使用 wordpress api 并且我知道 {{tag}} ... 我需要用 php 完成特定的事情。有没有办法将它预处理成部分文件?

最佳答案

如果你想在外部 php 文件(不是默认模板文件)中包含 wordpress 原生函数,你可以通过加载 wp-blog-header.phpwp 来加载 wordpress 默认函数-load.php 到那个文件

比如在最开始添加 require_once("/path/to/wordpress/wp-load.php");

$http请求获取json数据可以引用下面的代码,

var app = angular.module('recentPost', []);
app.controller('PostController',  ['$scope', '$http', function($scope, $http) {
    $http.get('your_json_url')
    .then(function(response) {
        console.log( response );    
        /*if ( response.data !== '-1' ) {
            $scope.lists= JSON.parse(response.data.data_received);
        } else {
            $scope.lists= response;
        }*/
    }
}]);

此外,我不认为直接在 wordpress 中使用 AngularJS 有什么意义,已经有 jQuery+Ajax 可以使用,没有必要加载额外的库

关于javascript - 如何在 AngularJS 部分文件中使用 php (wordpress) 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35285732/

相关文章:

javascript - 如何获取点击的html元素并进行编辑?

javascript - 为什么在分配给不带 var 关键字的变量时会出现引用错误?

php - cakePHP 在没有 session 、序列化或发布的情况下使用发布数据进行分页以获取

javascript - Jquery 单选按钮提交多次重复相同的值

预期页面重新加载时,AngularJS路由劫持非基本URL

javascript - Angular2(混合模式)无法将指令降级为 NG1(ES5)

javascript - Google 图表、更改图表类型以及显示/隐藏列

javascript - 如何从代码中动态删除div标签?

php - 读取目录然后插入到sql

javascript - 如何以树格式显示用户添加的文本