-
-
Save romuloctba/0249b34afb17b601cb0f to your computer and use it in GitHub Desktop.
This file contains hidden or 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>. | |
app.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(new_val) { | |
$translate(new_val).then(function (translation) { | |
iElm.html(translation); | |
}); | |
}); | |
} | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment