范围内的AngularJS变量 - 使用不同范围的变量

时间:2015-10-08 06:30:15

标签: angularjs scope

我对范围和可见性的变量有疑问。 目前我有两个控制器变量:

  

vm.selectedUser   vm.selectedUserRole

问题是,我在ng-repeat循环中生成表,并且不同表中的所有输入和选择共享上面提到的这两个变量。

我现在的问题是如何在我的方法中使用上述两个参数:

    vm.addInstitutionUserConnection(institutionUserConnectionContent.institution, institutionUserConnectionContent, selectedUser, selectedUserRole)

有没有机会这样做,因为输入,选择元素和span-button元素(添加用户)有不同的范围(这是我的猜测 - 我不确切地知道)

非常感谢您的帮助!

<div ng-repeat="institutionUserConnectionsOfInstitution in vm.institutionUserConnectionsOfInstitutions">
<div ng-repeat="institutionUserConnectionContent in institutionUserConnectionsOfInstitution.institutionUserConnectionContents">
    <div ng-show="addUser" class="row">
        <table>
           <tr>
                <td class="adduserinput">
                    <input 
                        type="text" 
                        class="form-control" 
                        placeholder="Benutzer durchsuchen" 
                        ng-model="vm.selectedUser"
                        typeahead="user as vm.userLabelFormatter(user) for user in vm.getUsersByTerm($viewValue) | limitTo:5"
                        typeahead-min-length="3"
                        typeahead-wait-ms="100"
                        typeahead-editable="false" />
                </td>
                <td class="adduserroles">
                    <select class="form-control" ng-model="vm.selectedUserRole" ng-options="role as role for role in vm.roles">
                    </select>
                </td>
                <td>
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button" ng-click="vm.addInstitutionUserConnection(institutionUserConnectionContent.institution, institutionUserConnectionContent)">add user</button>
                      </span>
                </td>
           </tr>
        </table>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

这样做 -

<div ng-repeat="institutionUserConnectionsOfInstitution in vm.institutionUserConnectionsOfInstitutions">
<div ng-repeat="institutionUserConnectionContent in institutionUserConnectionsOfInstitution.institutionUserConnectionContents">
    <div ng-show="addUser" class="row">
        <table>
           <tr>
                <td class="adduserinput">
                    <input 
                        type="text" 
                        class="form-control" 
                        placeholder="Benutzer durchsuchen" 
                        ng-model="institutionUserConnectionContent.selectedUser"
                        typeahead="user as vm.userLabelFormatter(user) for user in vm.getUsersByTerm($viewValue) | limitTo:5"
                        typeahead-min-length="3"
                        typeahead-wait-ms="100"
                        typeahead-editable="false" />
                </td>
                <td class="adduserroles">
                    <select class="form-control" ng-model="institutionUserConnectionContent.selectedUserRole" ng-options="role as role for role in vm.roles">
                    </select>
                </td>
                <td>
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button" ng-click="vm.addInstitutionUserConnection(institutionUserConnectionContent.institution, institutionUserConnectionContent, institutionUserConnectionContent.selectedUser, institutionUserConnectionContent.selectedUserRole)">add user</button>
                      </span>
                </td>
           </tr>
        </table>
    </div>
</div>

说明 -

我刚刚将vm更改为institutionUserConnectionContent。 原因 - 因为你在表格中有多行(ng-repeat),如果你使用vm,你最终会覆盖相同的selectedUser和一行的selectedUser与其他行。

希望这会有所帮助。