我可以将 ng-model 设置为两个值吗?
Can I set ng-model to two values?
我有一个输入
<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>
如果不行,有没有其他方法可以达到这个效果?
编辑:对于那些想知道的人,当我尝试时<p ng-bind="choice.text"></p>
由于某种原因它不起作用。
编辑 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 可能会干扰我的范围。也许在您尝试解决我的问题时请记住这一点。
使用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>
这不能接受吗?
<input type="text" ng-model="choice.text"></p>
<p ng-bind="choice.text"></p>
For now, just list[0]
. Eventually I will want them all to appear in a list but I can do that myself. For now I just want the first input to display in the bind.
要使列表的第一个出现在绑定中:
<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
中的每个 <div>
都有自己的子范围,choice
属性 设置为 poll.options[0]
、poll.options[1]
、poll.options[2]
,等等。只需在父范围内使用这些值即可。
我有一个输入
<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>
如果不行,有没有其他方法可以达到这个效果?
编辑:对于那些想知道的人,当我尝试时<p ng-bind="choice.text"></p>
由于某种原因它不起作用。
编辑 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 可能会干扰我的范围。也许在您尝试解决我的问题时请记住这一点。
使用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>
这不能接受吗?
<input type="text" ng-model="choice.text"></p>
<p ng-bind="choice.text"></p>
For now, just
list[0]
. Eventually I will want them all to appear in a list but I can do that myself. For now I just want the first input to display in the bind.
要使列表的第一个出现在绑定中:
<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
中的每个 <div>
都有自己的子范围,choice
属性 设置为 poll.options[0]
、poll.options[1]
、poll.options[2]
,等等。只需在父范围内使用这些值即可。