2015-04-23 71 views
2

在HTML:如何在angularJS指令中有條件地應用屬性?

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

我想在輸入標籤中的 「必需的」 詞只能說明,當它被設置當它被設置爲false時,該字就會消失。任何人可以幫忙?

+0

式兩份加入ng-required標籤http://stackoverflow.com/questions/13466133/how - 可以-I-有條件地需要外形輸入與 - angularjs – frhd

回答

3

使用ngRequired來控制Angular中的必需屬性。

更新您的模板來

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

這將有一個布爾值(angular input doc

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