2016-06-16 12 views
0

Parsing bekam ich eine Datenstruktur wie dieseeine Vorlage von einem Objekt in AngularJS

var abc = [ 
       {question: 'Do {{foo}} like <strong>you</strong>?'}, 
       {question: 'Do {{foo}} like <strong>fruits</strong>?'} 
      ]; 

Eine Vorlage wie diese

<h1>Question</h1> 
<span> {{question}} </span> 

Und in meinem Controller etwas wie folgt aus:

$scope.foo = 'Example', 
$scope.question = abc[0].question; 

Wie Sie wahrscheinlich wissen, funktioniert das nicht.

Wie könnte ich String zu einer Vorlage analysieren, um sie mit Variablen zu verwenden?

Antwort

1

können Sie verwenden ng-bind-html wie:

<ng-bind-html 
    ng-bind-html="expression"> 
... 
</ng-bind-html> 

aber diese Zusammenstellung erfordern.

Werfen Sie einen Blick auf diese Richtlinie: https://github.com/incuna/angular-bind-html-compile

Sie können es auf diese Weise verwenden:

<div bind-html-compile="question"></div> 

Ihre Frage wird für Sie injiziert und kompiliert werden.

+0

Dank für mich funktioniert –

1

Controller:

var foo = 'Example'; 

var abc = [ 
    {question: 'Do ' + foo + ' like <strong>you</strong>?'}, 
    {question: 'Do ' + foo + ' like <strong>fruits</strong>?'} 
]; 

$scope.question = abc[0].question; 

Ausblick:

<h1>Question</h1> 
<span ng-bind-html="question"></span> 
Verwandte Themen