在 AngularJS 中的 ng-repeat 循環中綁定 ng-model - Binding ng-model inside ng-repeat loop in AngularJS

問題:

I'm trying to deal with the issue of scope inside of an ng-repeat loop - I've browsed quite a few questions but have not quite been able to get my code to work.我正在嘗試處理 ng-repeat 循環內的範圍問題 - 我瀏覽了很多問題,但還沒有完全能夠讓我的代碼工作。

Controller code:控制器代碼:

function Ctrl($scope) {
  $scope.lines = [{text: 'res1'}, {text:'res2'}];
}

View:看法:

<div ng-app>
     <div ng-controller="Ctrl">
       <div ng-repeat="line in lines">
           <div class="preview">{{text}}{{$index}}</div>

       </div>
       <div ng-repeat="line in lines">
           <-- typing here should auto update it's preview above -->
           <input value="{{line.text}}" ng-model="text{{$index}}"/>
            <!-- many other fields here that will also affect the preview -->
       </div>
     </div>
    </div>

Here's a fiddle: http://jsfiddle.net/cyberwombat/zqTah/這是一個小提琴: http : //jsfiddle.net/cyberwombat/zqTah/

Basically I have an object (it's a flyer generator) which contains multiple lines of text.基本上我有一個包含多行文本的對象(它是一個傳單生成器)。 Each line of text can be tweaked by the user (text, font, size, color, etc) and I want to create a preview for it.用戶可以調整每一行文本(文本、字體、大小、顏色等),我想爲它創建一個預覽。 The example above only shows the input field to enter text and I would like that to automatically/as-you-type update the preview div but there will be many more controls.上面的例子只顯示了輸入文本的輸入字段,我希望它自動/按你輸入更新預覽 div,但會有更多的控件。

I am also not sure I got the code right for the looping index - is that the best way to create a ng-model name inside the loop?我也不確定我的代碼是否適合循環索引 - 這是在循環內創建 ng-model 名稱的最佳方法嗎?


解決方案:

參考: https://stackoom.com/en/question/ySxN
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章