2015-09-25 39 views
2

我需要幫助。 我有一個項目,我需要動態重複的html節點。這些節點有NG綁定和NG-模型屬性是這樣的:動態更改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)的魔力不會發生。 任何人都可以幫助我?

回答

0

使用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-模型$ scope.seq這將允許你控制你的span元素綁定到什麼地方。

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

所以,如果你輸入「」在你的輸入框中,將跨度標籤綁定到$ scope.quest.that,這等於「一些隨機值」

同樣,如果你鍵入「這」在你的輸入框中,將跨度標籤綁定到$ scope.quest.this,這等於「其他值」

+0

添加新的數據到你的追求對象將是非常容易的。要添加一個名爲「quest1」的新條目,您只需使用$ scope.quest.quest1 =「blah blah blah」 –

+0

然後,如果您在輸入框中鍵入了quest1,它會將「blah blah blah」綁定到您的span元素 –