我想知道如何在 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/