Home » Angularjs » How to convert dash-case to camelCase in AngularJS?

How to convert dash-case to camelCase in AngularJS?

Posted by: admin November 30, 2017 Leave a comment


Is there any utility function in AngularJS that converts a dash-case string to a camelCase string? e.g.: “min-max” becomes “minMax”

That’s quite simple to implement my own function:

function toCamelCase(name) {
  return name.replace(/-(\w)/g, function(match) {
    return match[1].toUpperCase();

But, I know that Angular already does this (e.g. directive name), so I wonder if it’s possible to use the mechanism that Angular has?


In angular this is part of jqlite, the minimalistic jquery implementation that they’re using internally. I don’t think there’s a way of accessing that because I believe it’s private. This is the function though:

function camelCase(name) {
  return name.
    replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
      return offset ? letter.toUpperCase() : letter;
    replace(MOZ_HACK_REGEXP, 'Moz$1');


var SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g;
var MOZ_HACK_REGEXP = /^moz([A-Z])/;


You can access to angular’s camelCase function inside of a directive:

link: function (scope, element, attrs) {
  attrs.$normalize('ng-model'); // ngModel
  attrs.$normalize('ng:click'); // ngClick
  attrs.$normalize('ng_show'); // ngShow