Skip to content

Instantly share code, notes, and snippets.

@olostan
Last active January 27, 2017 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olostan/23e9acc23480d0b377152889f782eeb6 to your computer and use it in GitHub Desktop.
Save olostan/23e9acc23480d0b377152889f782eeb6 to your computer and use it in GitHub Desktop.
Angular 1 substitution
<ui:substitute
placeholder-link-value='registration.link' placeholder-link-action='showTermsAndConditions(2)'
placeholder-user-value='registration.user'
placeholder-date-value='registration.date' placeholder-date-class="'stage-connect'"
placeholder-name-value='::name'>
The [link] have been agreed by [user] on [date], and name is [name]
</ui:substitute>
app.directive('uiSubstitute', function() {
return {
restrict:'E',
scope: false,
template: function(tElem, tAttr) {
let template = tElem.html();
const placeholders = {};
angular.forEach(tAttr, function(attrValue,attrKey) {
if (attrKey.substr(0,11)!='placeholder') return;
let [phKey, phType] = attrKey.substr(11).match(/([A-Z][a-z]*)/g);
phKey = phKey.toLowerCase();
if (attrValue.indexOf('"')>=0) throw "Please use single quotes: "+attrValue;
(placeholders[phKey]|| (placeholders[phKey] = {}))[phType]=attrValue;
});
angular.forEach(placeholders, function(phDef, ph) {
let elem = phDef.Action?'a':'span';
let attrs = [{key:'ng-bind', val:phDef.Value}];
if (phDef.Class) attrs.push({key:'ng-class',val:phDef.Class});
if (phDef.Action) attrs.push({key:'ng-click',val:phDef.Action});
attrs = attrs.map((v) => `${v.key}="${v.val}"`);
template = template.replace(`[${ph}]`,`<${elem} ${attrs.join(' ')}></${elem}>`);
});
return template;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment