material icon indicating copy to clipboard operation
material copied to clipboard

Input with placeholder not playing nicely with angular-translate

Open AntonTrapp opened this issue 11 years ago • 25 comments

I don't know if it is a angular-translate problem or a material problem, but the fix (https://github.com/angular/material/issues/1279) only is working for hardcoded labels (... placeholder="whatever" ...). If I use angular-translate (... placeholder="{{'key' | translate}}" ...) the result is fine if the field is not in focus, if I click on it the placeholder appears 2 times (looks like I am drunk lol).

AntonTrapp avatar Mar 02 '15 13:03 AntonTrapp

Please provide a CodePen or Plunkr that demonstrates this issue.

ThomasBurleson avatar Mar 12 '15 02:03 ThomasBurleson

Not with Plunkr. Tried to add material package on Plunkr - does nothing. Tried to copy the Getting started sample to Plunkr - not working there. A few minutes later Plunkr was offline.

Created a gist with CodePen: https://gist.github.com/anonymous/24a992448590489ce291

You can forget the CSS, the JS too (just provides a translation for the 'translate' filter.

In the form there are 2 input fields, one with a static label and the second with the translation via filter. provides the same generated HTML but looks ugly (label and placeholder if not in focus on the same position, blurry label if in focus). Tested on Chrome with this pen.

On Firefox the unfocused stuff is OK, but in focus the placeholder appers twice and blurry.

AntonTrapp avatar Mar 12 '15 07:03 AntonTrapp

The textarea is similarly broken with dynamic place holders.

KayEss avatar Mar 21 '15 06:03 KayEss

Can confirm the textarea problem.

As a workaround: When I use it inside the template of a directive

emptyLabel: '@?'
...
data-placeholder="{{emptyLabel}}")

and call it like this:

data-empty-label='{{"model.user.email_placeholder" | translate}}'

it is working for me. I have just some additional attribute stuff going on in the compile function (that has nothing to do with the input problem, if it's not working without the compile function try to add a dummy attribute to the input - that's what i'm doing). Maybe this is working for all people that are waiting for a fix or is helping the developers.

AntonTrapp avatar Mar 21 '15 07:03 AntonTrapp

"That is not an issue with translate. I have found your Pen http://codepen.io/anon/pen/OPBxBe and it clearly uses only a filter. In filters, no DOM operations are involved." - knalli #941 on angular-translate

So it is a material bug.

AntonTrapp avatar Apr 08 '15 19:04 AntonTrapp

yes - I have seen this as well. My temporary CSS workaround was this:

:-webkit-input-placeholder {
  opacity:0;
}
:-moz-placeholder {
  opacity:0;
}
::-moz-placeholder {
  opacity:0;
}
:-ms-input-placeholder {
  opacity:0;
}

denjello avatar Apr 29 '15 05:04 denjello

Thanks @denjello

zeeshanjan82 avatar Jun 15 '15 11:06 zeeshanjan82

This issue has nothing to do with angular-translate (or angular-gettext, whichever you prefer), it happens with any computed value for placeholder. For example, just

<md-input-container>
    <input placeholder="{{'Email'}}">
</md-input-container>

shows two placeholders on top of each other with slightly different font.

Using data-placeholder is a workaround but applies only on page load; its effectively a one-time binding. For example,

<md-input-container>
    <input data-placeholder="{{ctrl.email}}">
</md-input-container>

uses the value of ctrl.email on load but does not update if ctrl.email changes later on. That is: The data-placeholder attribute does change, but the value shown in the placeholder does not.

vbraun avatar Nov 03 '15 19:11 vbraun

any update ? ?

arsonik avatar Dec 10 '15 09:12 arsonik

Also having problems with md-input-container with angular-translate. Any news?

karlfock avatar Jan 27 '16 09:01 karlfock

+1 for a fix

jonaszuberbuehler avatar Feb 10 '16 14:02 jonaszuberbuehler

+1

thanuzz avatar Feb 23 '16 10:02 thanuzz

+1 Would be great =) Someone knows if there is a workaround ??

nicolasletoublon avatar Mar 01 '16 03:03 nicolasletoublon

+1. screen shot 2016-03-04 at 13 48 40

AlexanderKozhevin avatar Mar 04 '16 10:03 AlexanderKozhevin

I've created a workaround by extending the Angular Material Placeholder directive. I've created an overrule js file in my project with the following code:

(function () {
    'use strict'

    angular.module('app.core').config(['$provide', function ($provide) {
        $provide.decorator('placeholderDirective', ['$delegate', '$translate', function ($delegate, $translate) {
            var directive = $delegate[0];
            var link = directive.link;

            directive.compile = function () {
                return function (scope, element, attr, inputContainer) {
                    link.apply(this, arguments);

                    $translate(attr.placeholder).then(function(translation) {
                        inputContainer.element.find('label').text(translation);
                    });
                };
            };

            return $delegate;
        }]);
    }]);

})(); 

The placeholder attribute's value should be the translation key:

<input ng-model="myProfile.phone" placeholder="myProfile.phoneNumber" type="tel"  />

The decorator injects the $translate service. In the extended link function the translate service is called with the placeholder value and the label text is changed with the translate result.

md-no-float or static labels

If you're using the 'md-no-float' attribute or static labels you should add the following line to the 'then' callback function :

element.attr("placeholder", translation);

one-time binding vs dynamic binding

The above example is for one-time binding. If you also need the placeholder and labels to be dynamically changed when the language changes you could add a $translateChangeEnd listener and replace the compile return function with the following code:

return function (scope, element, attr, inputContainer) {
    link.apply(this, arguments);

    setTranslation();

    $rootScope.$on('$translateChangeEnd', function () {
        setTranslation();
    });

    function setTranslation() {
        $translate(attr.placeholder).then(function (translation) {
            inputContainer.element.find('label').text(translation);
        });
    }
};

Don't forget to also inject the '$rootScope' in the decorator. Please take into mind each input field has his own event listener. Don't know yet if this will cause performance issues. I think there should be better solutions for this.

Hope this helps!

thanuzz avatar Mar 09 '16 09:03 thanuzz

I am not sure if this is still an issue. Please take a look at this pen: http://codepen.io/bradrich/pen/bewLav.

It would seem that any introduction of a placeholder to a md-input-container input element along with a label simply puts the label above the input.

bradrich avatar Jun 20 '16 02:06 bradrich

@thanuzz I got your email with the updated pen (http://codepen.io/Thanuzz/pen/pbEVPP), however, it gave me a 404. I did follow your comments though and have reproduced the issue at http://codepen.io/bradrich/pen/bewLav. I will take a further look at this and let you know.

bradrich avatar Jun 20 '16 14:06 bradrich

@bradrich Yeah, sorry about that. I did reproduce the issue and commented on it but removed it again because on second thought I agreed with your conclusion 'I am not sure if this is still an issue'... The added variant is indeed a bug but is easily worked around by adding a translated label without the placeholder attribute on the input. I.m.o. this should be the preferred approach by default when you want a translated placeholder without a non-floating label.

With this approach I think every possible scenario is covered with placeholders/labels & translate. Conclusion could therefore be its a 'non-issue' when you mark the use of a translated placeholder without the label tag as a bad implementation.

Maybe you'd like to add this variant to your pen as well? Thanks!

      <md-input-container>
        <label translate="placeholder"></label>
        <input type="text" />
      </md-input-container>

thanuzz avatar Jun 21 '16 05:06 thanuzz

@thanuzz I have added the variant to the pen. I am seeing some weirdness in with this issue. I have forked the material repo and have made the changes in order to reproduce this issue and everything is working as expected. It is the strangest thing. Same versions of angular, material, etc.

Here is the code that is used to handle this type of placeholder, i.e. a placeholder without a label that is not on a md-select element:

if (element[0].nodeName != 'MD-SELECT') {
  var newLabel = angular.element('<label ng-click="delegateClick()" tabindex="-1">' + attr.placeholder + '</label>');
  attr.$set('placeholder', null);
  inputContainer.element
    .addClass('md-icon-float')
    .prepend(newLabel);

  $compile(newLabel)(scope);
}

If I had to make a guess, it would be that the translate filter delays the scope digest cycle and the above code is altering the placeholder and creating the new label prematurely. Using $timeout would normally be used to wait for the cycle to finish. This could explain how the new label is created, all of the classes are properly added, but then all of a sudden a random placeholder is put back in from the translate filter. The $timeout could be added to the functionality, but you have to wonder if fixing the 2% is worth risking the 98%.

My suggestion is that if you want a translated label, seek another method besides using the placeholder that will eventually become a label and use the label directly as we have in the pen. And, subsequently, this could be chalked up as you have said, as a bad implementation and not an issue...

bradrich avatar Jun 21 '16 13:06 bradrich

@bradrich Thanks for your extensive reply. I agree with your suggestion and personally would not add a $timeout for this. I remember back in March I changed the priorities of the translate and material input/placeholder directives trying to fix this but it didn't solved it then.

If you add a warning or fix, keep in mind both implementations below can be used, the latter if you're using variables in your placeholder text.

<input placeholder="{{ 'KEY' | translate }}" />
<input translate translate-attr-placeholder="WITH_VALUES" translate-values="{{values}}" />

thanuzz avatar Jun 23 '16 13:06 thanuzz