简体   繁体   English

HTML:使链接可编辑

[英]HTML: Making link editable

I have an angular page that I display a series of links on from a list. 我有一个角度页面,从列表中显示一系列链接。 Here's the HTML/Angular code: 这是HTML / Angular代码:

<li role="menuitem"
    ng-repeat="foo in foos track by $index"
    ng-click="selectFoo(foo)">
    <a href="#">
        {{foo}}
        <span class="glyphicon glyphicon-pencil pull-right"></span>
    </a>
</li>

I want the user to be able to edit the display name of foo by clicking on the glyphicon. 我希望用户能够通过单击glyphicon来编辑foo的显示名称。 I could do this pretty easily by adding an ng-click component to the <span> and opening up a modal with a text input etc etc. 我可以通过在<span>添加ng-click组件并使用文本输入等打开模态来轻松实现此目的。

But I want to do it in a more streamlined manner. 但我想以更简化的方式来做。 Is it possible to have the glyphicon click change the link into a text input where the user can type something, hit enter and finish editing. 是否有可能让glyphicon单击将链接更改为文本输入,用户可以在其中输入内容,点击Enter并完成编辑。 I don't want to make the <a> into a text input because clicking on it does another action. 我不想将<a>设为文本输入,因为单击它会执行其他操作。 Is there any way to achieve what I want? 有什么方法可以实现我想要的吗?

You could use AngularJS to switch between showing {{foo}} and showing an <input> that is binded to foo : 您可以使用AngularJS在显示{{foo}}和显示绑定到foo<input>之间切换:

<li role="menuitem"
    ng-repeat="foo in foos track by $index">
  <span data-ng-hide="foo.isEditing" data-ng-model="foo"></span>
  <input type="text" data-ng-show="foo.isEditing" data-ng-model="foo">
  <a data-ng-click="foo.isEditing = !foo.isEditing" href="#">
    <span class="glyphicon glyphicon-pencil pull-right"></span>
  </a>
</li>

So when a user clicks the glyphicon then it should toggle the state of foo.isEditing from false to true or vice-versa. 因此,当用户点击glyphicon那么就应该切换的状态foo.isEditingfalsetrue ,反之亦然。 Both span and input are binded to foo via ng-model so updates to foo happen instantaneously. spaninput都通过ng-model绑定到foo ,因此对foo更新会立即发生。 Let me know if this works out for ya, cheers! 让我知道这是否行得通,加油!

Setting the contentEditable property on your targeted span element should be the simplest solution one may desire. 在目标范围元素上设置contentEditable属性应该是人们想要的最简单的解决方案。 eg: 例如:

<span class="glyphicon glyphicon-pencil pull-right" contentEditable="true"></span>

Also, controlling it (most probably by implementing an on/off toggle ) through your script, would be as easy. 同样,通过脚本控制它(很可能通过实现on / off toggle)也很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM