rendering a dynamic placeholder with angular

你说的曾经没有我的故事 提交于 2019-12-01 06:26:53

问题


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.


回答1:


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;
      });
    }
  }
});


来源:https://stackoverflow.com/questions/22470089/rendering-a-dynamic-placeholder-with-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!