gpt4 book ai didi

angularjs - 如何在禁用 ng 的情况下使用 ng-if

转载 作者:行者123 更新时间:2023-12-01 23:40:45 24 4
gpt4 key购买 nike

下面是我的代码。

var myfriend = angular.module('myfriend',[]);

myfriend.controller('myfriendController', function($scope)
{
$scope.record = [
{ "id" : "01",
"firstname" : "Mohan ",
"middlename" : "K",
"lastname" : "Futterkiste1"
},{
"id" : "04",
"firstname" : "Rohan ",
"middlename" : "A",
"lastname" : "Futterkiste2"
},{
"id" : "08",
"firstname" : "sohan ",
"middlename" : "M",
"lastname" : "Futterkiste3"
}
]


});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

</head>
<body ng-app="myfriend">



<table class="table" style="border:1px red solid; width:100%; " ng-controller="myfriendController">
<thead>
<tr>
<th>Id</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in record">
<th>{{x.id}}</th>
<th><input type="text" ng-value="x.firstname"></th>
<th><input type="text" ng-value="x.middlename"></th>
<th><input type="text" ng-value="x.lastname"></th>
</tr>
<tr>
<td><button> Submit</button></td>
</tr>
</tbody>
</table>
<body>
</html>

我想要的是点击提交按钮,它应该只禁用 Rohan 字段(Rohan 是通过 ng-repeat 来的)。我想通过 ng-if 中的 ng-disabled 来实现这一点。

帮帮我。

最佳答案

你可以这样做

<tr ng-repeat="x in record">
<th>{{x.id}}</th>
<th><input type="text" ng-model="x.firstname" ng-disabled="x.disabled" value="x.firstname"></th>
<th><input type="text" ng-model="x.middlename" value="x.middlename"></th>
<th><input type="text" ng-model="x.lastname" value="x.lastname"></th>
</tr>

Controller

提交后给'Rohan'添加disabled属性

var myfriend = angular.module('myfriend',[]);

myfriend.controller('myfriendController', function($scope)
{

$scope.record = [
{ "id" : "01",
"firstname" : "Mohan",
"middlename" : "K",
"lastname" : "Futterkiste1"
},{
"id" : "04",
"firstname" : "Rohan",
"middlename" : "A",
"lastname" : "Futterkiste2"
},{
"id" : "08",
"firstname" : "sohan",
"middlename" : "M",
"lastname" : "Futterkiste3"
}
]

$scope.submit = function () {
angular.forEach($scope.record, function (v) {
if (v.firstname === 'Rohan') {
v.disabled = true;
}
});
console.log($scope.record);
}


});

工作 fiddle :JsFiddle

关于angularjs - 如何在禁用 ng 的情况下使用 ng-if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41762858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com