user7366018
user7366018

Reputation: 41

Add a variable with quotes to string in Javascript

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?

Upvotes: 2

Views: 1855

Answers (3)

Kirill A
Kirill A

Reputation: 517

$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.

Upvotes: 0

guest271314
guest271314

Reputation: 1

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}}'>`;

Upvotes: 0

Barmar
Barmar

Reputation: 781750

Use string concatenation.

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

Upvotes: 2

Related Questions