繁体   English   中英

重新加载ng-repeat角/离子

[英]Reload ng-repeat angular / ionic

我已经阅读了有关此问题的一些讨论,但没有人适合我。

我有一个控制器

.controller('TestFriendCtrl', ['$scope', 'APIUser', function($scope, APIUser) {

        $scope.data.friends = [{username: 'test'}];

        $scope.change = function(friend) {
            APIUser.find(friend, function(success, data){
                if (success) {
                    $scope.data.friends = data;
                    console.log($scope.data.friends);
                }
            })
        }
    }]);

我也试过

.controller('TestFriendCtrl', ['$scope', '$timeout', 'APIUser', function($scope, $timeout, APIUser) {

    $scope.friends = [{username: 'coucou'}];

    $scope.change = function(friend) {
        APIUser.find(friend, function(success, data){
            if (success) {
                $timeout(function() {
                    $scope.friends = data;
                    console.log($scope.friends);
                } );
            }
        })
        console.log($scope.friends);
    }
}]);

.controller('TestFriendCtrl', ['$scope', '$timeout', 'APIUser', function($scope, $timeout, APIUser) {

    $scope.friends = [{username: 'coucou'}];

    $scope.change = function(friend) {
        APIUser.find(friend, function(success, data){
            if (success) {
                $scope.friends = angular.copy(data);
            }
        })
        console.log($scope.friends);
    }
}]);

在所有情况下console.log($scope.friends); 返回期望值

和一个观点

<ion-view class="main-page">
    <ion-content>
        <h1>Friend</h1>
        <label class="item item-input">
            <i class="icon ion-search placeholder-icon"></i>
            <input ng-model="friend" name="friend" type="search" placeholder="Search" ng-change="change(friend)">
        </label>
        {{ data.friends[0].username }}
        <ion-list ng-controller="TestFriendCtrl" >
            <ion-item ng-repeat="friend in data.friends" class="item-thumbnail-left">
                <p>{{friend.username}}</p>
            </ion-item>
        </ion-list>
    </ion-content>
</ion-view>

$scope.friend在控制台输出中得到了很好的更新,但是我的列表没有改变。

我试图添加$scope.$apply但我已经有$digest already in progress

您可以尝试执行此操作,而我不知道APIUser会做什么:

.controller('TestFriendCtrl', ['$scope', 'APIUser', function($scope, APIUser) {
    $scope.friends = [{username: 'test'}];

    $scope.change = function(friend) {
        APIUser.find(friend).$promise.then(function(success, data){
            if (success) {
                $scope.friends = data;
                console.log($scope.friends);
            }
        }, function (err) {
            console.log(err);
        )};
    }
}]);

在您进行了最新更新之后,我认为可能会有指向的链接。 (点)。 在google中有很多容易找到的帖子,解释了为什么没有.dot dilimiter的直接模型为什么不受欢迎。

尝试使用$scope.myDatas.friends = data更改模型; 并将您的ng-repeat呼叫更改ng-repeat="friend in myDatas.friends"

暗示angular.copy()的注释几乎是正确的,只是缺少了第二个参数 使用angular.copy() (或类似的Lodash的 _.assign() ),您将不会丢失对原始数组实例的引用。 请参阅此答案此答案以获取进一步的说明。

.controller('TestFriendCtrl', ['$scope', 'APIUser', function($scope, APIUser) {
    $scope.friends = [{username: 'test'}];

    $scope.change = function(friend) {
        APIUser.find(friend).$promise.then(function(success, data){
            if (success) {
                angular.copy(data, $scope.friends);
                console.log($scope.friends);
            }
        }, function (err) {
            console.log(err);
        )};
    }
}]);

暂无
暂无

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

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