Last active
August 29, 2015 14:10
-
-
Save gabrielfgularte/8d8b0e934b254b46284a to your computer and use it in GitHub Desktop.
AngularBindTranslate (Extends http://angular-translate.github.io/)
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
// When using angular-translate it's impossible to make two-way data binding translations yet, | |
// and this sometimes can be a headache. So I've developed this directive to save some time. | |
// The view goes like this: <TAG data-bind-translate="scopeObjToTranslate.item.text"></TAG>. | |
angular.module('myApp', ['pascalprecht.translate']).directive('bindTranslate', [ | |
'$translate', | |
'$parse', | |
function($translate, $parse) { | |
return { | |
restrict: 'A', | |
link: function($scope, iElm, iAttrs) { | |
var arrayToTranslate = iAttrs.bindTranslate.split('.'), | |
objToTranslate = ''; | |
for (var i = 0; i < arrayToTranslate.length; i++) { | |
if (objToTranslate.length > 0) { | |
objToTranslate = objToTranslate + '.' + arrayToTranslate[i]; | |
} else { | |
objToTranslate = arrayToTranslate[i]; | |
} | |
}; | |
$scope.$watch(objToTranslate, function(newVal) { | |
$translate(newVal).then(function (translation) { | |
iElm.html(translation); | |
}); | |
}); | |
} | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice! o/