简体   繁体   English

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

[英]AngularJs. Data loaded via ajax doesn't shown in View

) I have a trouble with rendering ajax answer into View. 我将ajax答案渲染到View中时遇到了麻烦。

There is my 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>

And this is my controller 这是我的控制者

    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;
    };
});

Data comes from server as it should be, but not rendering into view( What i'm doing wrong? 数据来自服务器应该是,但不会呈现为视图(我做错了什么?

When you use tweets in ng-repeat directive, tweets must exist in current scope. 在ng-repeat指令中使用tweets时,推文必须存在于当前范围内。 You declare tweets in controller scope, but controller scope is limited by button element. 您在控制器范围内声明推文,但控制器范围受按钮元素的限制。 So the scope of ng-repeat know nothing about tweets. 所以ng-repeat的范围对推文一无所知。
Place you ng-controller directive on wrapper div of ng-repeat 将ng-controller指令放在ng-repeat的包装div上
HTML: HTML:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM