【发布时间】: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>
如果没有,有没有其他方法可以达到这个效果?
编辑:对于那些想知道的人,当我尝试<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 可能会干扰我的范围。在您尝试解决我的问题时,请记住这一点。
【问题讨论】:
-
为什么不
<p ng-bind="choice.text"></p>? -
您也可以添加
ng-bind部分吗? -
ng-repeat="choice in list"将有list.length输入和选择。你想在ng-repeat之外绑定哪个choice.text? -
现在,只列出 [0]。最终我会希望他们都出现在一个列表中,但我可以自己做到这一点。现在我只想在绑定中显示第一个输入。
标签: javascript html angularjs angular-ngmodel