Last active
January 27, 2017 15:27
-
-
Save olostan/23e9acc23480d0b377152889f782eeb6 to your computer and use it in GitHub Desktop.
Angular 1 substitution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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