1

有没有办法为每次重复 ng-repeat 发出回调。我有一个经过过滤的集合,每次显示项目时都需要动态修改文本。如果不可能,是否有任何解决方法?

<span ng-repeat="item in items|myfilter">
    <div nonexisting-ng-repeat-callback="getTextFromServer({{item.id}})"></div>
</span>

更新:

当我尝试以下操作时:

<span ng-repeat="item in items|myfilter">
    <div ng-style="{{item.style}}" ng-init="getTextFromServer(item)"></div>
</span>

在 $scope 中有这个

$scope.getColor = function (item) {
    var data = item.percentageComplete;
    if (data == 100) {
        return {
            color: "green"
        };
    } else {
        return {
            color: "orange"
        };
    }
};

$scope.getTextFromServer = function (item) {
        $http({
            method: 'GET',
            url: '/mypage',
            params : {"uri" : item.Url +"&Result=Percentage"}
        }).
        success(function (data, status, headers, config) {
            item.percentageComplete = data;
            item.style = $scope.getColor(item);
        }).
        error(function (data, status, headers, config) {
            console.log(data + " - " + status);
            item.percentageComplete = undefined;
            item.style = $scope.getColor(item);
        });
};

getTextFromServer 被调用,但是当 http 调用返回时,ng 样式不会更新。

问候, 林

4

2 回答 2

3

您可以使用ng-initorng-show指令:

<span ng-repeat="item in items|myfilter">
    <div ng-init="getTextFromServer({{item.id}})"></div>
</span>

或者

<span ng-repeat="item in items|myfilter">
    <div ng-show="condition"></div>
</span>
于 2013-06-26T09:04:17.260 回答
1

您可以使用ngInit指令:

<span ng-repeat="item in items|myfilter">
    <div ng-init="getTextFromServer({{item.id}})"></div>
</span>
于 2013-06-26T08:55:24.790 回答