ng-switch-when with ng-repeat

时间:2014-05-06 12:41:39

标签: angularjs angularjs-ng-repeat ng-switch

我的HTML代码是

<tr ng-switch-when="true" ng-repeat="onlineCredentials in onlineCredentialDetails" >
        <td>
            <span>Ordering Mode: </span>{{onlineCredentials.mode}}<br />
            <span>Intermedtiary Group: </span>{{onlineCredentials.name}}
        </td>
        <td>
            <span>Website: </span>{{onlineCredentials.webSite}}
        </td>
        <td >
            <span>Username / Password</span>
            <div ng-repeat="cred in onlineCredentials.loginDetails">
                -&nbsp;{{cred.userName}}&nbsp;/&nbsp;{{cred.password}}
            </div><br />
        </td>
    </tr>

哪个不是绑定数据...(TD和文本存在但绑定属性值为空)

如何一次使用ng-switch-when和ng-repeat?

提前致谢

2 个答案:

答案 0 :(得分:26)

ng-switch要求父母有

ng-switch on="variableName"

和孩子们有

ng-switch-when="stuff1" 

使用ng-repeat

的示例
<div ng-repeat="value in values" ng-switch on="value.color">
    <div ng-switch-when='blue'>Blue</div>
    <div ng-switch-when='green'>Green</div>
    <div ng-switch-when='orange'>Orange</div>
</div>

您的ng-repeat集合可能如下所示:

$scope.values = {
    one: {color: 'blue', worth: 2},
    two: {color: 'green', worth: 3},
    three: {color: 'orange', worth: 4},        
}

http://plnkr.co/edit/YavzpaPUBaBQNA0pHMPN?p=preview

答案 1 :(得分:1)

这是设计的。请参阅https://stackoverflow.com/a/15434085/1264360。基本上要修复你需要在ng-repeat上面的元素中使用ng-switch。