html - 就地编辑内容编辑

标签 html angularjs edit

当使用 ng-repeat 时,编辑内容的最佳方式是什么?

在我的理想情况下,添加的生日将是一个超链接,当点击它时,它将显示一个编辑表单 - 与当前带有更新按钮的添加表单相同。

Live Preview (Plunker)

HTML:

<!DOCTYPE html>
<html>
  <head lang="en">
    <meta charset="utf-8">
    <title>Custom Plunker</title>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
    <script>
      document.write('<base href="' + document.location + '" />');
    </script>
    <script src="app.js"></script>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.0/css/bootstrap-combined.min.css"
    rel="stylesheet">
  </head>
<body ng-app="birthdayToDo" ng-controller="main">
    <div id="wrap">

      <!-- Begin page content -->
      <div class="container">
        <div class="page-header">
          <h1>Birthday Reminders</h1>
        </div>
            <ul ng-repeat="bday in bdays">
                <li>{{bday.name}} | {{bday.date}}</li>
            </ul>

           <form ng-show="visible" ng-submit="newBirthday()">
            <label>Name:</label>
            <input type="text" ng-model="bdayname" placeholder="Name" ng-required/>
            <label>Date:</label>
            <input type="date" ng-model="bdaydate" placeholder="Date" ng-required/>
            <br/>
            <button class="btn" type="submit">Save</button>
        </form>
      </div>

      <div id="push"></div>
    </div>

    <div id="footer">
      <div class="container">
        <a class="btn" ng-click="visible = true"><i class="icon-plus"></i>Add</a>
      </div>
    </div>
    </body>

App.js:

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

app.controller('main', function($scope){ 

    // Start as not visible but when button is tapped it will show as true 

        $scope.visible = false;

    // Create the array to hold the list of Birthdays

        $scope.bdays = [];

    // Create the function to push the data into the "bdays" array

    $scope.newBirthday = function(){

        $scope.bdays.push({name:$scope.bdayname, date:$scope.bdaydate});

        $scope.bdayname = '';
        $scope.bdaydate = '';

    };
});

最佳答案

您应该将表单放在每个节点内,并使用 ng-showng-hide 分别启用和禁用编辑。像这样:

<li>
  <span ng-hide="editing" ng-click="editing = true">{{bday.name}} | {{bday.date}}</span>
  <form ng-show="editing" ng-submit="editing = false">
    <label>Name:</label>
    <input type="text" ng-model="bday.name" placeholder="Name" ng-required/>
    <label>Date:</label>
    <input type="date" ng-model="bday.date" placeholder="Date" ng-required/>
    <br/>
    <button class="btn" type="submit">Save</button>
   </form>
 </li>

这里的重点是:

  • 我已将控件 ng-model 更改为本地范围
  • ng-show 添加到 form 以便我们可以在编辑时显示它
  • 添加了一个带有 ng-hidespan 以在编辑时隐藏内容
  • 添加了一个 ng-click,它可以在任何其他元素中,将 editing 切换为 true
  • 更改了 ng-submit 以将 editing 切换为 false

这是您的 updated Plunker .

关于html - 就地编辑内容编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15453254/

相关文章:

jquery - 图像轮播在 body 上坍塌

html - 如何将填充或边距应用于显示为表格单元格的标题?

css - 使用 ng-click 存储和调用类

javascript - 使用 iframe 加载本地文件

c++ - 将字符串添加到 CEdit 文本框而不是 CCombobox

Javascript:如何更改节点名称?

php - 以 PHP 形式编辑 MYSQL 行

css - 我的 :hover is not working for some reason

php - Laravel 路线不适用于生产

javascript - 查找网页上唯一启用的按钮