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

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

问题描述

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

我在单独的 html 页面中有 PHP.我认为 PHP 会在每个页面出现之前触发 - 但我猜是因为页面是通过 angular 而不是浏览器加载的,这不会发生?

<p>这是第 1 页.</p><a href="#page2">转到第 2 页 </a><br><?php echo ('这个 php 不起作用');?><p>这个html在php下面</p>

虽然我使用页面,但同样的概念也适用于被带入视野的 div.无论如何在初始页面加载后使用 angular 触发 PHP?这可能吗?

解决方案

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

即使这样做了,AngularJS 也会缓存这些模板,因此它只会在服务器上运行"一次.这意味着如果有问题的模板被换出,那么模板使用的服务器上的数据发生变化,然后再次换回,对数据的更新不会反映在模板中,因为绝对为零Angular 方面的这些更新发生的知识.

像@Jonast92 这样的好主意在他的评论中说的是尽量不要混合客户端和服务器端的关注点,并在它们之间严格分离.在 Angular 应用程序的模板中使用 Angular 模型.而不是类似的东西:

description;?></p>

使用角度模型:

{{ item.description }}

如果您需要来自服务器的数据来执行此操作,请创建一个 Angular 服务并为您获取它:

angular.module('app').controller('controller', ['$scope', 'ItemManager',功能($范围,项目管理器){$scope.item = null;ItemManager.getItem('item-id').then(功能(项目){$scope.item = 项目;}, 功能() {console.log('加载项目失败');});}]);angular.module('app').service('ItemManager', ['$http', '$q',函数($http,$q){var svc = {获取项目:获取项目};返回 svc;函数 getItem(id) {var defer = $q.defer();$http.get('/items/' + id).成功(功能(数据){defer.resolve(数据);}).错误(功能(){defer.reject();});返回 defer.promise;}}]);

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.)

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>

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 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.

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.

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>

Use an angular model:

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

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天全站免登陆