programing

AngularJS: ng-read only

sourcejob 2023. 3. 10. 21:29
반응형

AngularJS: ng-read only

텍스트에 부울을 전달하는 데 이상한 문제가 있습니다.여기 jsfiddle이 있습니다.

<div ng-app ng-controller="MainCtrl">
    <p>First Name:
    <input ng-model="first" ng-readonly="true"/>
    </p>
    <p>Last Name:
        <input ng-model="second" ng-readonly="{{truefalse}}"/>
    </p>
</div>

function MainCtrl($scope) {
    $scope.first = "Angular";
    $scope.second = "JS";
    $scope.truefalse = "true";
}

왜 두 번째 필드를 수정할 수 있는지 설명해 주실 수 있나요?

스코프는 괄호 없이 ng-read only로 전달해야 합니다.$scope.truefalse는 문자열이 아니므로 따옴표는 필요 없습니다.

<div ng-app ng-controller="MainCtrl">
    <p>First Name:
    <input ng-model="first" ng-readonly="true"/>
    </p>
    <p>Last Name:
        <input ng-model="second" ng-readonly="truefalse"/>
    </p>
</div>

function MainCtrl($scope) {
    $scope.first = "Angular";
    $scope.second = "JS";
    $scope.truefalse = true;
}

속성으로 모델 내부 요소를 사용할 때는 항상 곱슬곱슬한 중괄호를 사용할 필요가 없습니다만, 아래는 당신의 경우 정상적으로 동작하지 않는 코드가 동작하는 코드입니다.

<!DOCTYPE html>
<html ng-app="convertApp">
<head>       
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>
<body>
    <div ng-app ng-controller="MainCtrl">
        <p>First Name:
            <input ng-model="first" ng-readonly="true"/>
        </p>
        <p>Last Name:
            <input ng-model="second" ng-readonly="{{truefalse}}"/>
        </p>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
    <script>
                        var app = angular.module('convertApp', []);
                        app.controller('MainCtrl', function ($scope) {
                            $scope.first = "Angular";
                            $scope.second = "JS";
                            $scope.truefalse = "true";
                        });
    </script>   
</body>

언급URL : https://stackoverflow.com/questions/20903649/angularjs-read-only-input-with-ng-readonly

반응형