6

I have a backend rendered template that returns a JSON object that contains a string that needs some dynamic data bindings for example...

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

By default the string is escaped, what's the best way in angular to convert dynamic_text to bind to $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>

Fiddle: http://jsfiddle.net/RzPM3/

1 Answer 1

5

You can use $interpolate module and easily achieve it like this

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

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.