【问题标题】:Can I set ng-model to two values?我可以将 ng-model 设置为两个值吗?
【发布时间】:2017-06-06 05:14:37
【问题描述】:

我有意见

<input type="text" ng-model="choice.text">

choice.text 在对象选择中创建一个新的文本属性。这允许我将输入的内容发送到不同的页面。虽然此功能有效,但我还想添加另一个功能,在页面上实时显示和更新输入内容。对this 的影响。

很遗憾,ng-model 已设置为 choice.text。有没有办法可以将两个值设置为一个ng-model?它可能看起来像这样:

<input type="text" ng-model="choice.text name"></p>
<p ng-bind="name"></p>

如果没有,有没有其他方法可以达到这个效果?

编辑:对于那些想知道的人,当我尝试&lt;p ng-bind="choice.text"&gt;&lt;/p&gt;它由于某种原因不起作用。

编辑 2:为了这个问题,我简化了代码。以下是更详细的实际代码:

<div class ="ask-container">
    <div ng-switch="cbstate">
      <div ng-switch-when="not-pressed">
        <h1>Choose between... </h1> <!-- I would like to add the dynamic element here -->
      </div>
      <div ng-switch-when="pressed">
        <h1>Hi</h1>
      </div>
    </div>
    <form role="form" ng-submit="submitChoice()">
      <div class="input" ng-repeat="choice in poll.options">
        <input type="text"  ng-model="choice.text" placeholder="Choice {{$index+1}}"><br>
      </div>
      <button class="add" type="button" ng-click="addChoice()">+</button>
      <button class="create" type="submit">Create</button>
    </form>
</div>

编辑 3:相同的代码,但使用 ng-bind:

<div class ="ask-container">
    <div ng-switch="cbstate">
      <div ng-switch-when="not-pressed">
        <h1>Choose between... </h1>
      </div>
      <div ng-switch-when="pressed">
        <h1>Hi</h1>
      </div>
    </div>
    <form role="form" ng-submit="submitChoice()">
      <div class="input" ng-repeat="choice in poll.options">
        <input type="text"  ng-model="choice.text" ng-change="temp=choice.text" placeholder="Choice {{$index+1}}"><br>
      </div>
      <button class="add" type="button" ng-click="addChoice()">+</button>
      <button class="create" type="submit">Create</button>
    </form>
    <p ng-bind="temp"></p>
    <p ng-bind="choice.text"></p>
</div>

注意:有人说 ng-repeat 可能会干扰我的范围。在您尝试解决我的问题时,请记住这一点。

【问题讨论】:

  • 为什么不&lt;p ng-bind="choice.text"&gt;&lt;/p&gt;
  • 您也可以添加ng-bind 部分吗?
  • ng-repeat="choice in list" 将有 list.length 输入和选择。你想在ng-repeat之外绑定哪个choice.text
  • 现在,只列出 [0]。最终我会希望他们都出现在一个列表中,但我可以自己做到这一点。现在我只想在绑定中显示第一个输入。

标签: javascript html angularjs angular-ngmodel


【解决方案1】:

现在,只需 list[0]。最终我会希望他们都出现在一个列表中,但我可以自己做到这一点。现在我只想在绑定中显示第一个输入。

让列表中的第一个出现在绑定中:

<div class="input" ng-repeat="choice in poll.options">
  <input type="text"  ng-model="choice.text" placeholder="Choice {{$index+1}}" />
  <br>
</div>

<p ng-bind="poll.options[0].text"></p>

ng-repeat 中的每个 &lt;div&gt; 都有自己的子范围,choice 属性设置为 poll.options[0]poll.options[1]poll.options[2] 等。只需在父范围中使用这些值。

【讨论】:

    【解决方案2】:

    使用 ng-change 更新其他模型

    <input type="text" 
           ng-model="choice.text" 
           ng-change="temp=choice.text"/> 
    

    现在进行测试,使用:

    <p ng-bind="temp"></p>
    <p ng-bind="choice.text"></p>
    

    【讨论】:

    • @为什么这是一个有效的解决方案?它抛出一个错误,并且 ans 收到 +1 票。请你发布一个工作代码。
    • 但最初的答案具有误导性。请在发布任何代码之前测试您的答案。
    • 是的。我在发布答案后进行了测试。道歉! #和平
    • 然后在 html 中添加

      ?
    • @Ethernetz,是的!
    【解决方案3】:

    这不能接受吗?

    <input type="text" ng-model="choice.text"></p>
    <p ng-bind="choice.text"></p>
    

    【讨论】:

    • 已编辑帖子。我在发布之前尝试过,但它不起作用:(
    • @Ethernetz 如果这不起作用,则意味着这两个元素之间的范围不同。他们在不同的控制器下吗?他们被ng-ifng-repeat 包围了吗?
    • 是的,它们被 ng-repeat 包围。这有什么影响吗?
    • 已编辑帖子,现在您可以看到正在使用 ng-repeat。
    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2014-12-01
    • 2016-07-28
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多