动态更改ng-bind和ng-model

Change ng-bind and ng-model dynamically

本文关键字:ng-model ng-bind 动态      更新时间:2023-09-26

我需要帮助。我有一个项目,我需要重复的html节点动态。这些节点具有ng-bind和ng-model属性,如下所示:

<input class="quest_model" type="text" data-ng-model="quest">
<span ng-bind="quest" class="quest_bind"></span>

我可以用Jquery改变这个属性,像这样:

quest.find("span.quest_bind").attr("ng-bind", "quest" + seq);

但是,当我按下一个按钮来复制一个节点时,angularJS(ng-bind)的魔力不会发生。有人能帮我吗?

使用json对象

$scope.quest = {
'this': 'some random value',
'that': 'some other value'
}
$scope.seq = 'this'

那么你只需要在控制器

中引用json对象
<span ng-bind="quest[seq]" class="quest_bind"></span>

设置输入的ng-model为$scope。Seq,它将允许你控制你的span元素绑定到什么。

<input class="quest_model" type="text" ng-model="seq">

所以如果你在输入框中输入"that",它会将span标签绑定到$scope.quest。,它等于"一些随机值"

同样,如果你在输入框中输入"this",它会将span标签绑定到$scope.quest。this,等于"some other value"