angularjs ng-repeat回调函数

时间:2013-06-26 08:09:24

标签: javascript angularjs

有没有办法在每次重复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样式。

此致 利恩

2 个答案:

答案 0 :(得分:3)

您可以使用ng-initng-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>

答案 1 :(得分:1)

您可以使用ngInit指令:

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