2017-04-11 85 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> 

您的解決方案無法正常工作,因爲刀片模板在角位前呈現。