【问题标题】:How to conditionally apply attribute in angularJS directive?如何有条件地应用 angularJS 指令中的属性?
【发布时间】:2015-07-01 06:02:12
【问题描述】:

在html中:

<cms-text required="false" id="product_name" thename="product_name"></cms-text>

在 cmsText.js 中

angular.module('cmsText',[]).directive('cmsText', function(){
'use strict';

    return {
        restrict: 'EA',
        scope: {
            thename:'=',
            required:'=',
            id:'='

        },
        replace:true,    
        templateUrl: 'cms-text/cmsText.html',
    };

});

在 cmsText.html 中

<input id="id" class="form-control" name="thename" type="text" required>

我希望输入标签中的“必填”词仅在设置为 true 时显示,而在设置为 false 时该词消失。有人可以帮忙吗?

【问题讨论】:

标签: angularjs angularjs-directive


【解决方案1】:

使用ngRequired 来控制Angular 中的必需属性。

将您的模板更新为

<input id="id" class="form-control" name="thename" type="text" ng-required="required">

【讨论】:

    【解决方案2】:

    这将添加带有布尔值 (angular input doc) 的 ng-required 标记

    <input id="id" class="form-control" name="thename" type="text" ng-required="required">
    

    【讨论】:

      猜你喜欢
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2016-05-22
      • 2017-11-19
      • 2016-09-26
      • 1970-01-01
      相关资源
      最近更新 更多