0

I have the following HTML:

<i class="icon-copy" data-clipboard data-clipboard-text="{{codeSnippet}}" data-title="Click to copy the code to your clipboard" data-placement="top"></i>
<textarea class="code-snippet" ng-model="codeSnippet" readonly onclick="this.focus();this.select()">&lt;script src="{{scriptURL}}"&gt;&lt;/script&gt;&lt;button class="main-button" style="background-color:{{button.color || branding.color}};border-radius:{{button.skin.radius || '0'}};display:none;" data-id="{{product.id}}" data-key="{{key}}" data-color="{{button.color || branding.color}}"&gt;{{button.label || 'Click Here'}}&lt;/button&gt;</textarea>

It takes some values the user has entered and builds up a script the user can embed in their site (for instance, button color, button label, button styling, etc). The interpolation is really handy for this.

However, I need to access this interpolated string to be available to data-clipboard-text so that I can copy it to the clipboard. The current way of trying to bind the code snippet using ng-model isn't working.

I would hate to have to build up the script tag in the controller using plain old JavaScript concatenation so I was wondering if there is a way to use interpolation to build up that string and add it to the scope.

I looked at $interpolate but I'm not sure if that's exactly what I need. Does $interpolate handle conditionals (like {{button.label || 'Click Here'}})?

$compile also looks like it could be relevant here, but I'm not sure how to put it all together (newish to Angular).

Hope this makes a sliver of sense. Thanks for any suggestions!

2
  • If the value can be interpolated, is it not already on your controllers $scope? Commented Aug 29, 2014 at 19:48
  • Each individual value is. However, I was hoping in the Javascript I could do something like: $scope.snippet = "<script src='{{scriptURL}}' ..... instead of building it up in the usual JS way: $scope.snippet = "<script src='" + scriptURL + .... Commented Aug 29, 2014 at 19:55

1 Answer 1

3

Following your comment, you can do:

// Create a function to parse your input and return an interpolated string
var getter = $interpolate('<script ng-src="{{scriptURL}}"></script>');
var value = {
    scriptURL: 'myScript.js'
};
// Evaluate the template with your values and add it to the $scope.
$scope.snippet = getter(value);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.