2

This is probably stupid simple, but I can't figure it out.

I am building a string in javascript

var template = '<div class="big-long-string-here">';

Now that works fine, but say I wanted to use ng-class with this div.

var template = '<div class="big-long-string-here" ng-class="{'open' : navCollapsed}">

Now, I'm escaping the string to place my class name, open in the string.

How would I pass this properly so it's rendered correctly in the DOM?

3
  • You could review whether it's appropriate to use template literals in your project. Commented Apr 20, 2017 at 23:05
  • Is open a variable whose value is supposed to be substituted into the string? Commented Apr 20, 2017 at 23:05
  • No, open should quite literally read 'open' Commented Apr 20, 2017 at 23:08

3 Answers 3

2

Use string concatenation.

var template = '<div class="big-long-string-here" ng-class="{' + open + ' : navCollapsed}">
Sign up to request clarification or add additional context in comments.

Comments

0

You can use template literal

var template = `<div class="big-long-string-here" ng-class='{"open" : "navCollapsed"}'>`;

If navCollapsed is a variable identifier

var template = `<div class="big-long-string-here" ng-class='{"open" : ${navCollapsed}}'>`;

7 Comments

I feel like your second solution is closest. However I need to be able to toggle the navCollapsed between true and false by clicking a button later on. That solution sets an indefinite value.
@user7366018 Not sure what you mean? You can change the attribute and the value of navCollapse at any time
It's sending this to the dom: ng-class="{"open" : false}"
That is not valid html for an attribute. If single quotes are used to surround value of attribute double quotes can be used inside of .value of attribute. Double quotes should not appear within double quotes of attribute value. What do you mean by "That solution sets an indefinite value"?
Once it's set to false, it can't be changed.
|
0

$templateCache.put('udt-toolbar.html', '<div ng-class="{{datatable.config.name ? \'your-class\' : \' \'}}" name="udt-toolbar" class="button-toolbar-main clearfix"></div>');

This is how can be resolved.

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.