Search code examples
javascriptangularjsplaceholder

rendering a dynamic placeholder with angular


I've looked around, found several resources labeled 'ng-placeholder' or something incredibly similar. I cannot get this to work:

<input type="text" placeholder="{{option.name}}" class="form-control" ng-switch-when="text" />

I've noticed there doesn't appear to be anything on the input documentation as well. I'm pretty new to angular, and this has done nothing but frustrate me for a few hours. There must be a way to do this.


Solution

  • Why not write your own directive for ng-placeholder? Something simple like this should work. You can call it in your html like this

    <input ng-placeholder='test'>
    

    Where test is a scope variable in the current controller.

    .directive('ngPlaceholder', function($document) {
      return {
        restrict: 'A',
        scope: {
          placeholder: '=ngPlaceholder'
        },
        link: function(scope, elem, attr) {
          scope.$watch('placeholder',function() {
            elem[0].placeholder = scope.placeholder;
          });
        }
      }
    });