AngularJs。通过ajax加载的数据未在View中显示

时间:2013-11-15 06:02:30

标签: javascript angularjs

) 我在将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;
    };
});

数据来自服务器应该是,但不是渲染到视图中( 我做错了什么?

1 个答案:

答案 0 :(得分:3)

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

<div id="content" layout:fragment="content" ng-app="Messages" ng-controller="getMoreTweets">