【问题标题】:Replacing label with input textbox and vice versa by clicking a button in AngularJs通过单击AngularJs中的按钮用输入文本框替换标签,反之亦然
【发布时间】:2016-05-24 14:08:42
【问题描述】:

我有一个带有标签和按钮编辑的表单,当我单击按钮时,标签应转换为文本框,标签数据为文本,当我保存时,文本框应再次转换为标签。

在 AngularJs 中我们如何处理?谁能提供一些这方面的信息?

html:

<form ng-app="testApp" ng-controller="testController">
    <label class="keyColumn">name: </label>
    <label class="valueCoulmn">stackover flow </label>
    <button ng-click="editLabel">Edit</button>
</form>

Controller.js:

(function() {
  angular
    .module("testApp", [])
    .controller('testController', function($scope) {
      $scope.editLabel = function() {}
    });
})();

【问题讨论】:

  • 请提供一个您迄今为止更接近尝试的示例。这样我们就可以一起帮助解决这个问题。

标签: javascript angularjs


【解决方案1】:

您可以使用ngshowngHide 显示/隐藏标签和输入。
基本上&lt;label&gt; 应该包含一个显示数据的表达式,对应的&lt;input&gt; 应该包含指向相同数据的ngModel。然后使用按钮简单地在显示模式之间切换:

angular.module('test', []).controller('testCtrl', function($scope) {
  $scope.editMode = false;
  $scope.name = "John Doe";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test" ng-controller="testCtrl">
  <form>
    <label ng-hide="editMode">{{name}}</label>
    <input ng-show="editMode" ng-model="name">
    <button ng-click="editMode=true">Edit</button>
    <button ng-click="editMode=false">Save</button>
  </form>
</div>

如果您认为您的表单非常繁重,并且不想在 DOM 中同时拥有 &lt;label&gt;&lt;input&gt;,请改用 ngIf

【讨论】:

    【解决方案2】:

    您可以在label 标签中使用contenteditable 属性。

    查看此演示:

    (function() {
      "use strict";
    
      var app = angular.module("app", []);
      app.controller("Controller", ["$scope",
        function($scope) {
          $scope.buttonText = "Edit";
    
          $scope.editSave = function(evt) {
            var label = evt.target.previousElementSibling; // Get the label tag from your button.
            var labelData = label.innerText; // Get the label text.
    
            alert(labelData);
    
            if ($scope.buttonText == "Edit") { // If the current button's text is "Edit"...
              label.setAttribute("contenteditable", true); // Set contenteditable=true to your label.
    
              /* To make focusable your editable label. */
              label.setAttribute("target", 0);
              label.focus(); // Set the focus in your label.
              $scope.buttonText = "Save"; // Change the button's text to "Save".
            } else {
              label.removeAttribute("contenteditable");
              label.removeAttribute("target");
              $scope.buttonText = "Edit";
            }
          };
        }
      ]);
    })();
    label {
      padding: 2px;
    }
    label[contenteditable=true] {
      border: solid 1px #CCCCCC;
      padding: 2px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div data-ng-app="app">
      <div data-ng-controller="Controller">
        <form id="form">
          <div>
            <label>Label</label>
            <button data-ng-bind="buttonText" data-ng-click="editSave($event)" type="button"></button>
          </div>
        </form>
      </div>
    </div>

    此演示适用于您的上次更新:

    (function() {
      angular
        .module("testApp", [])
        .controller('testController', function($scope) {
          $scope.buttonText = "Edit";
    
          $scope.editLabel = function(evt) {
            var label = evt.target.previousElementSibling; // Get the label tag from your button.
            var labelData = label.innerText; // Get the label text.
    
            alert(labelData);
    
            if ($scope.buttonText == "Edit") { // If the current button's text is "Edit"...
              label.setAttribute("contenteditable", true); // Set contenteditable=true to your label.
    
              /* To make focusable your editable label. */
              label.setAttribute("target", 0);
              label.focus(); // Set the focus in your label.
              $scope.buttonText = "Save"; // Change the button's text to "Save".
            } else {
              label.removeAttribute("contenteditable");
              label.removeAttribute("target");
              $scope.buttonText = "Edit";
            }
          };
        });
    })();
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <form ng-app="testApp" ng-controller="testController">
      <label class="keyColumn">name:</label>
      <label class="valueCoulmn">stackover flow</label>
      <button ng-bind="buttonText" ng-click="editLabel($event)"></button>
    </form>

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2019-12-27
      • 2021-11-16
      相关资源
      最近更新 更多