最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to show and hide the element attribute based on boolean value in angularjs? - Stack Overflow

programmeradmin1浏览0评论

ctrl.js

In controller the value is true if user is landing on edit page

var self = this;
self.edit = "true"

html

On add page it should be

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

On edit page it should be

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

For disable I can use ng-disabled=ctrl.edit to make input field disabled how, to do for directive custome-directive-to-check-unique-value

ctrl.js

In controller the value is true if user is landing on edit page

var self = this;
self.edit = "true"

html

On add page it should be

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

On edit page it should be

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

For disable I can use ng-disabled=ctrl.edit to make input field disabled how, to do for directive custome-directive-to-check-unique-value

Share Improve this question asked Nov 26, 2015 at 11:17 Mr XMr X 1,7493 gold badges30 silver badges57 bronze badges 2
  • self.edit = "true" ? as string , or boolean – Shushanth Pallegar Commented Nov 26, 2015 at 11:19
  • It is a boolean field and its working fine just need how to remove custom directive .. able to do the field disabled perfectly – Mr X Commented Nov 26, 2015 at 11:22
Add a ment  | 

3 Answers 3

Reset to default 2

You can simply use the attribute ng-show or ng-hide based on the value.

<div ng-hide="edit">Edit is False</div>

<div ng-show="edit">Edit is True</div>

If edit is true then the ng-show would be true showing the message Edit is True

You can use two input fields and use ng-if to show one of the fields based on a bool value which specify whether it is add view or edit view.

<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>
  1. you can use ng-disabled directive to disable or enable your input text, but as i see you have done this already.

    1. As for the directive disable, why dont you use an if statement inside your directive body and check what you need to do according to your ctrl.edit value.

to do this just pass the ctrl.edit from your input text to your directive like this:

html

<input name="games" ng-model="ctrl.game" ng-required="true" ng-maxlength="50" custome-directive-to-check-unique-value   myFlag = "ctrl.edit">

directive

.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
                     }
                }
            };
        })

hope helps,good luck.

发布评论

评论列表(0)

  1. 暂无评论