根据特定条件使用ng-show / ng-hide显示/隐藏元素

时间:2015-04-01 22:07:13

标签: javascript html angularjs

我有以下内容:

<input class="form-field form-control" type="text" 
     name="Website" ng-model="vm.infodata.Website"
     placeholder="Website Address" maxlength="50"
     required ng-pattern="/^(www\.)?[a-zA-Z0-9_\-]+\.([a-zA-Z]{2,4}|[a-zA-Z]{2}\.[a-zA-Z]{2})(\/[a-zA-Z0-9\-\._\?\&=,'\+%\$#~]*)*$/"
     ng-readonly="vm.isreadonly" ng-disabled="vm.applyDisabled">
     <div class="form-field form-control disabled" >
         <a target="_blank" href='http:\\{{vm.infodata.Website}}'>{{vm.infodata.Website}} </a>
     </div>

基本上,要求是,如果用户在输入中输入URL并在下次加载页面时保存,则它将显示为链接而不是输入字段。   - 如果页面加载时vm.infodata.Website中没有数据,则显示输入并隐藏链接。   - 如果页面加载时vm.infodata.Website中有数据隐藏文本框并显示链接   - 如果单击编辑按钮,则隐藏链接并显示文本框

此字段是必填字段。但是,有些电子邮件地址字段需要以相同的方式运行,但不是必需的字段。有没有办法在角度中执行此操作而不将一堆布尔值设置为true和false并隐藏/显示基于该字段的不同字段?

1 个答案:

答案 0 :(得分:0)

我最后改变了一下代码。

我现在有一个带有占位符的输入字段,用于单击“编辑”按钮时

 <input class="form-field form-control" ng-show="vm.visibleBoolean" required placeholder="Credit Union Website" 
                                       ng-pattern="/^(www\.)?[a-zA-Z0-9_\-]+\.([a-zA-Z]{2,4}|[a-zA-Z]{2}\.[a-zA-Z]{2})(\/[a-zA-Z0-9\-\._\?\&=,'\+%\$#~]*)*$/" 
                                       name="CUWebsite" type="text" ng-model="vm.cuinfodata.Website" maxlength="50" 
                                       ng-readonly="vm.isreadonly" ng-disabled="vm.applyDisabled">

然后我正在使用ng-bind-html:

<div class="form-field form-control disabled" ng-hide="vm.visibleBoolean" ng-bind-html="cuWebsite"></div>

在控制器中,如果没有数据我正在这样做:

$scope.cuWebsite = "<span class='placeholder'>Your Website</span>"

当有数据时,我正在这样做:

$scope.cuWebsite = "<a href='http://" + vm.cuinfodata.Website + "' target='_blank'>" + vm.cuinfodata.Website + "</a>";

单击编辑按钮时,将设置ng-hide属性。它可能不是一种优雅的方式,但它符合要求。