search

Home  >  Q&A  >  body text

angular.js - How does angular add a field to determine a boolean value?

As follows:<input type="text" readonly="ifReadOnly()"/>
readonly="expression"

How to obtain it from the controller in expression?

if($scope.index == “0”){
                
                $scope.ifReadOnly=function(){
                    
                    return false;
                }
            }else{
                
                $scope.ifReadOnly=function(){
                    
                    return true;
                }
            }

readonly="expression" cannot be written as readonly="ifReadOnly()".

How to modify it so that readonly can work according to different values ​​of expression?

PHP中文网PHP中文网2839 days ago1127

reply all(3)I'll reply

  • 習慣沉默

    習慣沉默2017-05-15 17:04:07

    Let’s do this:

    <input type="text" readonly="ifReadOnly"/>
    $scope.$watch('index', function(newIndex){
        $scope.ifReadOnly = newIndex !== '0';
    });

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-15 17:04:07

    html:

    <input type="checkbox" ng-model="demo.readOnly" ng-change="handleRead()">

    js:

    // 默认值
    $scope.demo = {
        readOnly: true
    };
    
    $scope.handleRead = function () {
        if ($scope.demo.readOnly) {
            // todo readonly
        } else {
            // todo not readonly
        }
    };
    

    reply
    0
  • ringa_lee

    ringa_lee2017-05-15 17:04:07

    用ng-readyonly, https://docs.angularjs.org/api/ng/directive/ngReadonly

    reply
    0
  • Cancelreply