如何在angularjs中显示和隐藏基于布尔值的元素属性
How to show and hide the element attribute based on boolean value in angularjs?
ctrl.js
在控制器中,如果用户登录编辑页面,则该值为true
var self = this;
self.edit = "true"
html
在添加页面上,它应该是
<md-input-container flex="25">
<label>GameId</label>
<input name="games" ng-model="ctrl.game" ng-required="true" ng-maxlength="50" custome-directive-to-check-unique-value >
</md-input-container>
在编辑页面上,它应该是
<md-input-container flex="25">
<label>GameId</label>
<input name="games" ng-model="ctrl.game" ng-required="true" ng-maxlength="50" disabled>
</md-input-container>
对于禁用,我可以使用ng-disabled=ctrl.edit
使输入字段禁用方式,为指令custome-directive-to-check-unique-value
ng-show
或ng-hide
。
<div ng-hide="edit">Edit is False</div>
<div ng-show="edit">Edit is True</div>
如果edit为true,则ng show将为true,显示消息Edit is True
您可以使用两个输入字段,并使用ng-if
根据布尔值显示其中一个字段,布尔值指定是添加视图还是编辑视图。
<input ng-if="!ctrl.edit" name="games" ng-model="ctrl.game" ng-required="true" ng-maxlength="50" custome-directive-to-check-unique-value >
<input ng-if="ctrl.edit" name="games" ng-model="ctrl.game" ng-required="true" ng-maxlength="50" disabled>
您可以使用
ng-disabled
指令来禁用或启用您的输入文本,但正如我所看到的,您已经这样做了。- 至于指令禁用,为什么不在指令体中使用if语句,并根据
ctrl.edit
值检查需要做什么
- 至于指令禁用,为什么不在指令体中使用if语句,并根据
要做到这一点,只需将输入文本中的ctrl.edit
传递给指令,如下所示:
html
<input name="games" ng-model="ctrl.game" ng-required="true" ng-maxlength="50" custome-directive-to-check-unique-value myFlag = "ctrl.edit">
指令
.directive('customeDirectiveToCheckUniqueValue', function () {
return {
restrict: 'AE',
scope:{
myFlag :'='
},
link: function (scope, element, attrs) {
//here make your condition and add your code accordingly
if(myFlag){
//if true your code
}else{
//if false your code
}
}
};
})
希望有帮助,祝你好运。
相关文章:
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 有条件地在选项标记中应用布尔属性
- 如何“;默认“;JavaScript中的布尔属性
- 查找数组's按属性不存在于另一个数组中的对象
- 为什么属性存在于对象实例上,即使其原型发生了更改
- AngularJS:必需属性适用于某些字段,但不适用于其他字段
- 奇怪的数据属性布尔问题
- 角度布尔属性未在指令外部更新
- 选择数据属性日期值早于当前服务器日期的元素
- Realm React Native-需要迁移:目标类型'字符串'不会'不存在于属性'电
- 在布尔属性JQuery之间切换
- 依赖于其他属性的 JavaScript 属性值
- C#等价于Python属性自定义/Javascript对象文本
- 如何使用JavaScript添加布尔属性
- 通过angular js智能表中的多个布尔属性进行过滤
- 当聚合物元素中的布尔属性发生变化时,如何用sort重新运行dom repeat
- 用JavaScript读取本地文件:单个文件等价于files属性
- 指令的onblclick:切换其布尔属性值
- 如何添加布尔属性指令
- 将类应用于带有属性的元素