2017-04-11 43 views
0

我使用的是angular 1.5和laravel 5.3,我有一个控制器,它包含一个questions数组,并且我也将这个数组传递给视图。 我试图做的是:试图在刀片模板引擎内使用angular 1.5变量

<div ng-if="questions.length > 0"> 
      <md-card ng-repeat="question in questions"> 
       <md-card-content> 
        <h4><% question.title %></h4> 
        <p ng-bind-html="renderHtml(question.content)"></p> 
        <p>{{ $questions['<% question.id %>']->created_at->diffForHumans() }}</p> 
       </md-card-content> 
      </md-card> 
     </div> 

但它像刀片引擎无法得到<%question.id%>值。 顺便说一句,我从{{}}更改为< %%>。 感谢

+0

您对此有何看法?刀片服务器端编译。在Angular在客户端编译之前。 – estus

回答

0

你可以做这样的事情:

QuestionsController:

$questionsForAngular = []; 

foreach($questions as $question) { 
    $questionsForAngular[] = $question->created_at->diffForHumans(); 
} 

角控制器:

$scope.setQuestions = function(questions) { 
    $scope.questionsFromBackend = questions; 
} 

斜视:

<div ng-if="questions.length > 0" ng-init='setQuestions({{ $questionsForAngular }})'> 
    <md-card ng-repeat="question in questions"> 
     <md-card-content> 
      <h4><% question.title %></h4> 
       <p ng-bind-html="renderHtml(question.content)"></p> 
       <p><% questionsFromBackend %></p> 
     </md-card-content> 
    </md-card> 
</div> 

您的解决方案无法正常工作,因为刀片模板在角位前呈现。