The ng-readonly Directive in AngularJS is used to specify the readonly attribute of an HTML element. The HTML element will be readonly only if the expression inside the ng-readonly directive returns true. The ng-readonly directive is required to change the value between true and false. In case, if the readonly attribute is set to false, then the presence of the readonly attribute makes the element readonly, irrespective of its value.
Syntax:
Contents...
Parameter:
- expression: It returns true if the expression sets the element's readonly attribute.
This directive is supported by , elements.
Example 1: This example uses ng-readonly Directive to enable readonly property.Â
<html>
<head>
<title>ng-readonly Directivetitle>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js">
script>
head>
<body ng-app style="text-align:center">
<h1 style="color:green">GeeksforGeeksh1>
<h2>ng-readonly Directiveh2>
<div>
<label>Check to make month readonly:
<input type="checkbox" ng-model="open">
label><br><br>
Input Month:
<input ng-readonly="open"
type="month"
ng-model="month">
div>
body>
html>
Output:
Example 2: This example uses the ng-readonly Directive to enable the readonly property in option list.Â
<html>
<head>
<title>ng-readonly Directivetitle>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js">
script>
head>
<body ng-app="app" style="text-align:center">
<h1 style="color:green">GeeksforGeeksh1>
<h2>ng-readonly Directiveh2>
<div ng-controller="geek">
<div>
<button ng-click="edit=true">Editbutton>
<button ng-init="edit=false" ng-click="edit=false">
Update
button>
div>
<br>
<div>Select sorting algorithmdiv><br>
<div>
<select class="form-control"
ng-disabled="!edit"
ng-init="status=1"
ng-model="status"
ng-options="s.id as s.set for s in set">
select>
div>
div>
<script>
let app = angular.module("app", []);
app.controller('geek', ['$scope', function ($scope) {
$scope.set = [{ id: 1, set: 'Merge sort' },
{ id: 2, set: 'Quick sort' },
{ id: 3, set: 'Bubble sort' }];
}]);
script>
body>
html>
Output: