AngularJs。使用ng repeat和ng开关渲染表格

时间:2013-11-21 05:34:05

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

我正在使用angularjs指令尝试使用两列的渲染表。

<table>
<tr ng-repeat="message in messages" >
    <td ng-switch-on="message.network" ng-switch when="twitter" ng-controller="TweetController">
        <span>
            <input type="checkbox" name="active" ng-checked="{{message.isPublished}}" data-id="{{message.socialId}}" ng-click="publishMessage($event)" />
        </span>
        <span>
            <img src="{{message.authorPicture}}" />
        </span>
        <span>{{message.createdAt}}</span>
        <span>{{message.network}}</span>
        <span>{{message.text}}</span>
    </td>
    <td ng-switch-on="message.network" ng-switch when="facebook" ng-controller="FacebookController">
        <span>
            <input type="checkbox" name="active" ng-checked="{{message.isPublished}}" data-id="{{message.socialId}}" data-createdAt="{{message.createdAt}}" ng-click="publishMessage($event)" />
        </span>
        <span>
            <img src="{{message.authorPicture}}" />
        </span>
        <span>{{message.createdAt}}</span>
        <span>{{message.network}}</span>
        <span>{{message.text}}</span>
    </td>
</tr>
</table>

应该有2列 - 首先应该是twitter消息,第二应该是facebook帖子。但在我的情况下,两个列中都会显示相同的消息(如此http://puu.sh/5oHT7.png)。我做错了什么?

1 个答案:

答案 0 :(得分:2)

为了简单起见,只需重组模型:

$scope.messages = [
    [
        {
            "network": "twitter",
            ...
        },
        {
            "network": "facebook",
            ...
        }
    ],
    [
        {
            "network": "twitter",
            ...
        },
        {
            "network": "facebook",
            ...
        }
    ]       
]

HTML:

<table>
    <tr ng-repeat="message in messages" >
        <td ng-repeat="msg in message">
            <div ng-if="msg.network == 'twitter'">
                <div ng-controller="TweetController">
                    ...
                    <span>{{msg.createdAt}}</span>
                    <span>{{msg.network}}</span>
                    <span>{{msg.text}}</span>
                </div>
            </div>
            <div ng-if="msg.network == 'facebook'">
                <div ng-controller="FacebookController">
                    ...
                    <span>{{msg.createdAt}}</span>
                    <span>{{msg.network}}</span>
                    <span>{{msg.text}}</span>
                </div>
            </div>
        </td>       
    </tr>
</table>