1

) 我在将 ajax 答案呈现到 View 时遇到了麻烦。

有我的看法

    <div id="content" layout:fragment="content" ng-app="Messages">
    <div class="twitter" ng-repeat="tweet in tweets" ng-model="tweets">
                <span> <input type="checkbox" name="active"
                    ng-checked="isSelected($event)" class="publishCheckbox"
                    data-id="{{tweet.socialId}}" ng-controller="publishController"
                    ng-click="publishMessage($event)" /></span> <span>{{tweet.createdAt}}</span>
                <span>{{tweet.network}}</span> <span>{{tweet.text}}</span>
            </div>
            <button type="button"
                class="btn btn-default navbar-btn twitter-posts"
                ng-controller="getMoreTweets" ng-click="onloadTweets($event)">Get
                more tweets</button></div>

这是我的控制器

    var app = angular.module("Messages", []);
app.controller('getMoreTweets', function($http, $scope, $element) {
    $scope.tweets = [];
    $scope.onloadTweets = function(e) {
        var buttonElement = angular.element(e.target);
        var socialId = buttonElement.prev().prev().children().children().attr(
                "data-id");
        var dataString = {
            'socialId' : socialId
        };
        $http({
            method : 'GET',
            url : "/infoboard/getMoreTweets",
            params : dataString
        }).success(function(data, status, headers, config) {
            $scope.tweets = data;
            console.log(data);
            console.log($scope.tweets);
            console.log("All correct");
        }).error(function(data, status, headers, config) {
            console.log("Something went wrong");
        });
    };
    $scope.isSelected = function(e) {
        return $scope.selected.indexOf(angular.element(e.target)) >= 0;
    };
});

数据应该来自服务器,但没有呈现到视图中(我做错了什么?

4

1 回答 1

3

当您在 ng-repeat 指令中使用推文时,推文必须存在于当前范围内。您在控制器范围内声明推文,但控制器范围受按钮元素的限制。所以 ng-repeat 的范围对推文一无所知。
将 ng-controller 指令放在 ng-repeat
HTML 的包装 div 上:

<div id="content" layout:fragment="content" ng-app="Messages" ng-controller="getMoreTweets">
于 2013-11-15T06:33:11.430 回答