在angular中运行PHP [英] Running PHP within angular

查看:149
本文介绍了在angular中运行PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angular在wordpress网站中创建页面过渡.我的网站加载了一个普通的wordpress页面,该页面触发了PHP,并在页面中填充了角元素(正文).然后,角度元素使用动画过渡以3个单独的html页面更改正文内容(因此页眉和页脚不受影响.)

I am using angular to create page transitions in a wordpress site. My site loads a normal wordpress page which fires its PHP and populates the page with angular elements (the body). The angular elements then use animated transitions to change the body content with 3 separate html pages (so header and footer are unaffected.)

我在单独的html页面中有PHP.我以为PHP会在每个页面进入视图之前触发-但是我想是因为页面是由angular而不是浏览器加载的,这不会发生吗?

I have PHP in the separate html pages. I thought the PHP would trigger before each page came into view - but im guessing because the pages are being loaded by angular and not the browser, this doesn't happen?

<div id="pageone">
    <p>This is page 1.</p>
    <a href="#page2">Go to page 2 </a><br>

    <?php echo ('this php does not work'); ?>

    <p>This html is below php</p>
</div>

尽管我使用页面,但相同的概念适用于被引入视图的div.无论如何,在初始页面加载后是否使用angular来触发PHP?这有可能吗?

Although I use pages, the same concept applies to divs being brought into view. Is there anyway to fire PHP using angular after the initial page load? Is this possible at all?

推荐答案

AngularJS完全是客户端.您可以将PHP放在HTML模板中,但是如果您未将网络服务器配置为将HTML文件解析为PHP,那么您的PHP甚至都不会被解析.

AngularJS is completely client side. You can put PHP in your HTML templates, but if you don't configure your webserver to parse HTML files as PHP then your PHP isn't even going to be parsed.

即使这样做,AngularJS也会缓存这些模板,因此它将仅在服务器上一次运行".这意味着,如果交换出所涉及的模板,然后在该模板所使用的服务器上更改数据,然后又将其交换回去,则对数据的更新将不会反映在模板中,因为绝对为零有关这些更新的Angular方面的知识.

Even if it did, AngularJS caches these templates so it will only be 'run' on the server a single time. This means if the template in question is swapped out, then data changes on the server that the template makes use of and then it is swapped back in again, the updates to the data are not going to be reflected in the template because there's absolutely zero knowledge on Angular's side of these updates occurring.

一个类似@ Jonast92的好主意在他的评论中说,是不要将客户端和服务器端的问题混合在一起,并在它们之间进行严格的分隔.在角度应用程序的模板中使用角度模型.而不是像这样:

A good idea like @Jonast92 says in his comment is to try not to mix client-side and server-side concerns and enforce a strict separation between them. Use Angular models in your angular application's templates. Instead of something like:

<p><?php echo $item->description; ?></p>

使用角度模型:

<p>{{ item.description }}</p>

如果您需要来自服务器的数据来执行此操作,请使Angular服务出去并为您获取数据:

If you need data from the server in order to do this, make an Angular service to go out and get it for you:

angular.module('app').controller('controller', [
    '$scope', 'ItemManager',
    function($scope, ItemManager) {
        $scope.item = null;

        ItemManager.getItem('item-id').then(
            function(item)  {
                $scope.item = item;
            }, function() {
                console.log('load item failed');
            }
        );
    }
]);

angular.module('app').service('ItemManager', [
    '$http', '$q',
    function($http, $q) {
        var svc = {
            getItem: getItem
        };

        return svc;

        function getItem(id) {
             var defer = $q.defer();
             $http.get('/items/' + id)
                 .success(function(data) {
                     defer.resolve(data);
                 })
                 .error(function() {
                     defer.reject();
                 })
             ;

             return defer.promise;
        }
    }
]);

这篇关于在angular中运行PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆