简体   繁体   English

Angular js Variable不适用于Twig

[英]Angular js Variable is not working with Twig

problem 问题

  • im using twig template & angular js. 即时通讯使用树枝模板和角度js。

controller 控制者

 public function testAction()
{
  $this->render('AcmeDemoBundle:abc.html.twig');
 }

js code js代码

    var scotchApp = angular.module('scotchApp', ['ngRoute']);

 angular.module('scotchApp', [])
.config(['$interpolateProvider', function ($interpolateProvider) {
  $interpolateProvider.startSymbol('[[');
 $interpolateProvider.endSymbol(']]');
 }]);

 scotchApp.config(function($routeProvider)
{
   $routeProvider
   //route for home page
           .when('/', {
               templateUrl:'http://localhost/example/example/web/app_dev.php/home',
                controller:'mainController'

        })
        //route for the about page
                .when('/about',{
                 templateUrl:'http://localhost/example/example/web/app_dev.php/about1',
                 controller:'aboutController'

        })
         //route for the contact page
                .when('/contact',{
                 templateUrl:'http://localhost/example/example/web/app_dev.php/contact',
                 controller:'contactController'

        });
});
// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {

    // create a message to display in our view
    $scope.message = 'Everyone come and see how good I look!';
});

html code HTML代码

 <div class="jumbotron text-center" ng-model="test">
    <h1>About Page</h1>

    <p>{{ '{{'~ message ~'}}' }}</p>
 </div>
  • Its seems like twig is treating variable (message to be symfony rendered through test controller 好像树枝正在处理变量(消息是通过测试控制器渲染的symfony)

  • whereas we have not rendered message variable in twig through test controller. 而我们尚未通过测试控制器在树枝中呈现消息变量。

  • i knew problem is twig is conflicting with angular js variable 我知道问题是树枝与角度js变量冲突

  • therefore i have googled enabled changes ,but as if its not working 因此,我已经用谷歌搜索启用了更改,但是好像没有用

it can be possible through seperating syntax (twig/angular js) 通过单独的语法(twig / angular js)可以做到

  link : https://docs.angularjs.org/api/ng/provider/$interpolateProvider

update 更新

 {% verbatim %}
  <div ng-app="scotchApp" ng-controller="aboutController">
    {{message}}
 </div>

   {% endverbatim %}
  • now {{message}} is printing on browser ,angular is not working on that case. 现在{{message}}正在浏览器上打印,angular在这种情况下不起作用。

what i need 我需要的

    i need to render message variable to twig file through angular js 

Angular can be mixed with symfony,but not always is a good idea . 角度可以与symfony混合使用,但并非总是一个好主意。 In your example yuo give 在你的例子中

 $interpolateProvider.startSymbol('[[');
 $interpolateProvider.endSymbol(']]');

That means that for angular in twig you need to use [[variable]] instead of {{variable}} 这意味着对于树枝中的角度,您需要使用[[variable]]而不是{{variable}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM