ng-repeat和静态类型代码之间的行为有何不同?

时间:2013-11-16 19:08:04

标签: angularjs angularjs-ng-repeat angularjs-model

html与此ng-repeat代码产生的行为有什么区别:

        <div id="settings-row-{{key}}" class='settings-row' ng-repeat="(key, value) in items">
          <div id='settings-source-{{key}}' class='settings-source settings-column'>{{key}}</div>
          <div class='settings-slider settings-column'>
            <input type="range" ng-model="items[key]" min="0" max="4" />
          </div>
        </div>

和这个输入的代码:

       <div id="settings-row-science" class='settings-row'>
          <div id='settings-source-science' class='settings-source settings-column'>science</div>
          <div class='settings-slider settings-column'>
            <input type="range" ng-model="items['science']" min="0" max="4" />
          </div>
        </div>
        <div id="settings-row-english" class='settings-row'>
          <div id='settings-source-english' class='settings-source settings-column'>english</div>
          <div class='settings-slider settings-column'>
            <input type="range" ng-model="items['english']" min="0" max="4" />
          </div>
        </div>
        <div id="settings-row-math" class='settings-row'>
          <div id='settings-source-math' class='settings-source settings-column'>math</div>
          <div class='settings-slider settings-column'>
            <input type="range" ng-model="items['math']" min="0" max="4" />
          </div>
        </div>

我试图让第一个ng-repeat代码与第二个输出代码具有相同的行为,但由于某种原因,第一个代码的滑块坚持并且不会顺利滑动。但是,第二个代码的滑块可以平滑滑动。有什么区别我没有考虑到?任何建议,将不胜感激。

items是:

    {"science":0,"english":3,"math":4}

1 个答案:

答案 0 :(得分:1)

我认为这可能是一些问题,无论如何,要使其工作,请尝试将模型定义为列表:

$scope.items = [{
    "name": "science",
    "value": "0"
}, {
    "name": "english",
    "value": "3"
}, {
    "name": "math",
    "value": "4"
}];

其中HTML是:

<div ng-controller="fessCntrl">
    <div id="settings-row-{{item.name}}" class='settings-row' ng-repeat="item in items">
        <div id='settings-source-{{item.name}}' class='settings-source settings-column'>{{item.name}}</div>
        <div class='settings-slider settings-column'>
            <input ng-model="item.value" type="range" min="0" max="4" />
        </div>
    </div> <pre>{{items|json}}</pre>
</div>

演示 Fiddle