【问题标题】:Clear input text AngularJS清除输入文本AngularJS
【发布时间】:2014-10-09 06:49:14
【问题描述】:

一旦发现错误,我会在输入控件中使用 Glyphicon 图标。 Glyphicon 图标上有错误和正确的图像。但我想在点击错误字形图标时清除输入控件。 以下是我的代码:

 <div class="{{layout=='horizontal'? fieldClass || 'col-lg-9':''}} ">
        <div style="position:relative" class="input-group">
            <span class="input-group-addon"><b>PD</b></span>
            <div class="icon-addo addon-md">
                <input type="text" class="form-control {{textboxClass}}" name="textControl" id="textControl" ng-readonly="readonly" ng-required="required"
                       maxlength="10" placeholder="{{placeholder || layout=='inline' ? labelText : ''}}" data-ng-model="vm.value" ng-blur="vm.onBlur()" />

                <i ng-show="vm.state==1" class="form-control-feedback fa fa-spinner fa-spin" style="width: auto; height:auto; font-size: 25px; top:5px; right:10px"></i>
                <i onclick="clearControl(this,'textControl');" class="form-control-feedback" ng-class="{'glyphicon glyphicon-ok': vm.state==2, 'glyphicon glyphicon-remove': vm.state==3}" style="width: auto; height:auto; font-size: 20px; top:0; right:10px"></i>
            </div>
        </div>
        <div ng-show="textForm['textControl'].$error.required && (submitted || !textForm['textControl'].$pristine)" class="help-block ng-hide">{{labelText}} is required</div>
    </div></i>

请帮忙。

【问题讨论】:

    标签: javascript angularjs controls


    【解决方案1】:

    不要使用内联 onclick 处理程序。您可以简单地使用ngClick 指令清除输入:

    <i ng-click="vm.value = ''" class="form-control-feedback" ng-class="{'glyphicon glyphicon-ok': vm.state==2, 'glyphicon glyphicon-remove': vm.state==3}" style="width: auto; height:auto; font-size: 20px; top:0; right:10px"></i>
    

    我还建议清除 HTML,将样式移动到 CSS 文件中,这将使您的代码更全面。

    【讨论】:

    • 实际上后来我使用了 ng-click 但在这里我想调用一个函数而不是直接清除字段,因为我还想检查控制器中的一些其他值。但你的回应有帮助。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    相关资源
    最近更新 更多