用angular渲染一个动态的占位符 [英] rendering a dynamic placeholder with angular

查看:150
本文介绍了用angular渲染一个动态的占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,发现了一些标有"ng-placeholder"或类似内容的资源.我无法使它正常工作:

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" />

我注意到输入文档中似乎没有任何内容.我对angular还是很陌生,这什么都没做,只是让我沮丧了几个小时.必须有一种方法可以做到这一点.

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.

推荐答案

为什么不为ng-placeholder编写自己的指令?像这样简单的事情应该起作用.您可以像这样在您的html中调用它

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'>

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

这篇关于用angular渲染一个动态的占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆