6

たとえば、いくつかの動的データ バインディングを必要とする文字列を含む JSON オブジェクトを返すバックエンド レンダリング テンプレートがあります。

sampleLogic = {
  "1": "Sample static text and some {{ dynamic_text }}." 
}

デフォルトでは、文字列はエスケープされます.angularでdynamic_textを変換して$scope.dynamic_textにバインドする最良の方法は何ですか?

JS:

 var sampleLogic = {
    "1": "Sample static text and some {{ dynamic_text }}."
};

function parseMe($scope) {
    $scope.copy = sampleLogic['1'];
    $scope.dynamic_text = "dynamic text woooot";
}

HTML:

<div ng-app>
    <div ng-controller="parseMe">
        <div ng-bind-html-unsafe="copy"></div>
    </div>
</div>

フィドル: http://jsfiddle.net/RzPM3/

4

1 に答える 1

5

$interpolateモジュールを使用して、このように簡単に実現できます

var dynamic_text = {
    'dynamic_text': "dynamic text woooot"
};
$scope.copy = $interpolate(sampleLogic['1'])(dynamic_text);

DEMO

于 2013-08-19T15:33:03.487 に答える