当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - are angular controllers within an ng-repeat section the same instance?

问题描述:

I am trying to have a section of html and an associated controller repeat, but what I am finding is that the controllers seem to be a single object. That is, when I change an attribute of one of the controllers it changes for all of them. Am I doing something wrong or is this the intended behavior?

Here is a simplified jsfiddle which demonstrates the issue. I want each "upgrade slot" to have it's own selectedUpgrade value, but it seems that they are all connected.

网友答案:

The slotCtrl is set on the district div, not on the slot div. So each repeated slot uses the same controller. See https://jsfiddle.net/hzzq5j3h/

Instead of

<div ng-repeat="district in mainCtrl.districts" 
     ng-controller="slotController as slotCtrl">
    <h4>{{district.name}} slots:</h4>
    <div ng-repeat="slot in district.slots">
        ...
    </div>
</div>

You want

<div ng-repeat="district in mainCtrl.districts">
    <h4>{{district.name}} slots:</h4>
    <div ng-repeat="slot in district.slots" ng-controller="slotController as slotCtrl">
        ...
    </div>
</div>
分享给朋友:
您可能感兴趣的文章:
随机阅读: