angular-string-interpolation
inˈtərpəˌlāt/ - verb: insert (something) between fixed points
This module was created to help developers create a separation between the text for a project and the actual project code. Currently you can easily abstract static text out into a constant or configuration file, but if that abstracted string needs any dynamic information you will be forced to split the copy up into many smaller parts or leave the copy directly in the DOM.
This module allows you to keep marketing or interface copy out of the DOM while still allowing you to use dynamic data.
Comments and Pull Requests welcome!
Contents
Installation
NPM
npm install angular-string-interpolation --save
Bower
bower install angular-string-interpolation --save
Manual
Dependencies
- Angular.js (~1.4.0)
How Replacement Works
This module uses the same basic format as ES6 template strings: ${0}
. Rather
than naming the custom variables as you would with template strings, these placeholders simply take
an integer for each replacement with the first replacement starting with 0
. These numbers will be
used to get the correct content from the passed in array.
<!-- Output: Item 0 matches ZERO, item 2 matches ONE, etc...-->
You can use as many instances of a placeholder as needed:
<!-- Output: How much wood would a woodchuck chuck if a wood chuck could chuck wood?-->
Usage
Include bc.AngularStringInterpolation
as a dependency in your project.
angular;
Directive
Use the directive as an element or as an attribute:
<!-- As an element --> <!-- Output: Who is Statler without Waldorf?--> <!-- Or as an attribute --> <!-- Output: Who is Hobbes without Calvin?-->
bc-string
This custom attribute accepts a string containing the items to be replaced.
// You can define all the content inside your controllerthismyString = 'You have ${0} dollars in your ${1} account.';
<!-- Output: You have 200 dollars in your checking account.-->
Or pass a string directly to the attribute:
<!-- Output: Who is Garth without Wayne?-->
bc-array
This custom attribute accepts an array containing the items to be injected into the placeholders. As
with bc-string
you can define items in the controller or directly in the DOM.
// You can define the content inside your controllerconst remainingCredits = 12;const expiration = 'Aug 31st, 2016';thisreplacements = remainingCredits expiration;
<!-- Output: You have 200 dollars in your checking account.-->
Or pass an array directly to the attribute:
<!-- Output: Who is Bullwinkle without Rocky?-->
Service
The interpolation method is exposed through bcInterpolationService
. This allows you to interpolate
text inside a controller, service or anywhere it is needed.
Parameters
string
:{String}
- A string containing placeholders (
${}
)
- A string containing placeholders (
values
:{Array}
- An array of values to replace the placeholders
String
Returns // Controller Example // Inject the service into your controller { 'ngInject'; // Define our string and the values to inject into the placeholders thisstring = 'You have ${0} credits remaining as of ${1}.'; thisvalues = '12' ; // Pass both into the service to get back an interpolated string thisstring = bcInterpolationService; }
Filter
The interpolation method is also exposed through the bcInterpolation
filter. This allows several
uses.
Parameters
string
:{String}
- A string containing placeholders (
${}
)
- A string containing placeholders (
values
:{Array}
- An array of values to replace the placeholders
String
Returns // Controller Example { // Define our string and the values to inject into the placeholders thisstring = 'You have ${0} credits remaining as of ${1}.'; thisvalues = '12' ; // Pass both into the filter to get back the interpolated string thisfilteredString = thisstring values; // Outputs: // "You have 12 credits remaining as of Fri Jul 08 2016 16:45:19 GMT-0400 (EDT)." }
{{ vm.string | bcInterpolation:vm.values }} <!-- or --> {{ "Neither ${0} nor ${1} nor ${2} nor gloom of ${3}..." | bcInterpolation:['❄', '🌧', '🔥', '🌃'] }} <!-- Outputs: Neither ❄ nor 🌧 nor 🔥 nor gloom of 🌃...-->
Development
npm run build
- Build JS/CSS/HTML/SVGnpm run build:js
- Build JSnpm run watch:js
- Watch JS/HTML and rebuild on changenpm run watch
- Watch JS/HTML and rebuild on change