问题描述
如下:<input type='text' readonly='ifReadOnly()'/>readonly='expression'
expression中如何根据控制器来获得呢,
if($scope.index == “0”){$scope.ifReadOnly=function(){return false;} }else{$scope.ifReadOnly=function(){return true;} }
readonly='expression' 写成readonly='ifReadOnly()'不可以的。
如何修改才能让readonly根据expression不同的值去产生作用呢?
问题解答
回答1:这样吧:
<input type='text' readonly='ifReadOnly'/>
$scope.$watch(’index’, function(newIndex){ $scope.ifReadOnly = newIndex !== ’0’;});回答2:
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 }};回答3:
用ng-readyonly, https://docs.angularjs.org/api/ng/directive/ngReadonly