【问题标题】:AngularJS not working with PHP Slim + TwigAngularJS 不适用于 PHP Slim + Twig
【发布时间】:2016-07-11 20:07:05
【问题描述】:

我有一个使用 AngularJS 的简单页面。它加载一些数据,填充选择框,并且应该对选择这些选择框中的值做出反应。

它与直接链接完美配合:

http://warcry.ru/codiad/workspace/jw/templates/arena-counter-picks.html

但它不适用于 Slim 路由:

http://warcry.ru/codiad/workspace/jw/public/jw/arena

我的 Slim 应用程序中有“jw/arena”路由。

_http://warcry.ru/codiad/workspace/jw/public/index.php renders
_http://warcry.ru/codiad/workspace/jw/templates/arena-counter-picks.html.

我该如何解决这个问题?感谢您的帮助。

编辑:

看来我找到了这个问题的原因。我不只使用 Slim,我将它与 Twig 一起使用。并且看起来 Twig 与 AngularJS 冲突,因为它们的语法相似。

那么如何将它们耦合在一起呢?或者我根本不应该一起使用它们?

找到答案:AngularJS-Twig conflict with double curly braces

【问题讨论】:

  • 请问你用的是哪个slim版本?

标签: php angularjs twig slim


【解决方案1】:

使用 angularjs 选择框试试下面的 slim 框架 2 示例,它 对我有用:

我有学生姓名表,我将在选择框中显示学生姓名,因此请创建一个学生表或任何您的表:

我的带有 ng-app 的 index.html 文件是:

<html ng-app="mainApp">
    <head>
        <script type="text/javascript" src="angular.min.js"></script>
        <script type="text/javascript">
        var app=angular.module('mainApp',[]);
        angular.module('mainApp').controller('myCtrl1',function($scope,$http){
         $http({
            method : "GET",
            url : "http://localhost/em/mod.php/devicegrp",
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function mySucces(response2) {
            $scope.mydata2=response2.data;
            },function myError(response2) {
            $scope.mydata2=response.statusText;
          });
        });

        </script>
    </head>
    <body ng-controller="myCtrl1">
    <select class="form-control" ng-options="o.id as o.studentnm for o in mydata2" ng-model="valueselected" ></select>selected:{{valueselected}}
    </body>
</html>

而我的 Slim framework 2 文件是这样的:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,OPTIONS,POST,PUT,DELETE');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
require 'vendor/autoload.php';
$app->get('/get', function () use ($app) {
      $sql = "select * from student";
        try {
          $db = getConnection();
          $stmt = $db->query($sql);
          $us = $stmt->fetchAll(PDO::FETCH_OBJ);
          $db = null;
          echo json_encode($us);
        } 
        catch(PDOException $e) {
          echo '{"error":{"text":'.$e->getMessage() .'}}';
        }
      });
  function getConnection() {
  $dbhost="localhost";
  $dbuser="root";
  $dbpass="";
  $dbname="restdb";
  $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  return $dbh;
}

$app->run();

?>

这样试试

【讨论】:

    【解决方案2】:

    看来我找到了这个问题的原因。我不只使用 Slim,我将它与 Twig 一起使用。并且看起来 Twig 与 AngularJS 冲突,因为它们的语法相似。

    那么如何将它们耦合在一起呢?或者我根本不应该一起使用它们?

    【讨论】:

      猜你喜欢
      • 2015-08-05
      • 2018-08-29
      • 1970-01-01
      • 2018-02-13
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多