Format

The format service provides access to the following functions:

  • formatText(formatString, args) — Formats the args with a given format string.
  • escape(text) — Replaces the <, >, and & HTLM characters with &lt;, &gt;, and &amp;.

Demo

Formatted result: {{formatCtrl.formattedResult}}
HTML-stripped result: {{formatCtrl.htmlResult}}

Markup

<div ng-controller="FormatServiceController as formatCtrl">
    <div class="row">
        <div class="col-md-6">
            <div class="input-group">
                <label for="formattedText">Enter some text</label>
                <input class="form-control" type="text" name="formattedText" ng-model="formatCtrl.formattedText" />
            </div>
            <div>Formatted result: {{formatCtrl.formattedResult}}</div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="input-group">
                <label for="htmlText">Enter some HTML</label>
                <input class="form-control" type="text" name="htmlText" ng-model="formatCtrl.htmlText" />
            </div>
            <div>HTML-stripped result: {{formatCtrl.htmlResult}}</div>
        </div>
    </div>
</div>

JavaScript

/*global angular */

(function () {
    'use strict';

    function FormatServiceController($scope, bbFormat) {
        var self = this;

        self.formattedResult = '';
        self.formattedText = '';
        self.htmlText = '';
        self.htmlResult = '';

        $scope.$watch(function () {
            return self.formattedText;
        }, function () {
            self.formattedResult = bbFormat.formatText('[{0}]', self.formattedText);
        });

        $scope.$watch(function () {
            return self.htmlText;
        }, function () {
            self.htmlResult = bbFormat.escape(self.htmlText);
        });
    }

    FormatServiceController.$inject = ['$scope', 'bbFormat'];

    angular.module('stache')
    .controller('FormatServiceController', FormatServiceController);
}());