gpt4 book ai didi

javascript - 如何使用 AngularJS 在单击时将文本元素更改为输入字段?

转载 作者:行者123 更新时间:2023-11-28 11:05:50 26 4
gpt4 key购买 nike

我想知道如何在 AngularJS 中做到这一点。

此关键字列表及其旁边有一个编辑按钮。

    <tr ng-repeat="keyword in keywords">
<td>
<strong id="keyword.name">{{ keyword.name }}</strong>
</td>
<td>
<button ng-click="editKeyword(keyword.name)">Edit</button>
<button ng-click="deleteKeyword(keyword.name)">Delete</button>
</td>
</tr>

现在在我的 Controller 中我有这样的东西。

$scope.editKeyword = function(name){
console.log(name);
//something done to change the <strong> element into a text input
};

如何通过 Controller 将“strong”元素替换为文本输入字段。这可以在 angularJS 中完成吗?

感谢您的帮助。

最佳答案

正如查理提到的,ng-if 可以完成这项工作。还有“ng-switch”,它就是专门针对这种情况而设计的。

<tr ng-repeat="keyword in keywords">
<td ng-switch="mode[$index]">
<input ng-switch-when="edit" id="edit" ng-model="keyword.name">
<strong ng-switch-default id="keyword.name">{{ keyword.name }}</strong>
</td>
<td>
<button ng-click="editKeyword(keyword.name, $index)">Edit</button>
<button ng-click="deleteKeyword(keyword.name)">Delete</button>
</td>
</tr>

Controller 看起来像:

$scope.editKeyword = function(name, index){
$scope.mode[index] = "edit";
console.log(name);
//something done to change the <strong> element into a text input
};

完成编辑后,将 $scope.mode[index] 更改为其他任何内容。

关于javascript - 如何使用 AngularJS 在单击时将文本元素更改为输入字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38191441/

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