javascript - 如何在 ng-repeat 中启动 forEach 函数

标签 javascript angularjs json

我想在 ng-repeat 上运行一个函数,该函数获取 JSON 中 Qprogress 对象的值并将其转换为百分比。我已经正确编写了该函数,但我不知道如何启动它。我尝试将其包装在作用域内的 forEach 中,然后使用作用域作为我想要修改的元素的 ng-model,但它不起作用。 这是 HTML:

<tr ng-repeat="item in clients" progressCalc>
    <td>
        <a href="#/details/{{clients.indexOf(item)}}" title="Link to {{item.FirstName}} {{item.LastName}}" class="oly-open">{{item.FirstName}} {{item.LastName}}</a>
    </td>
    <td ng-hide="item.Progress == 'In Progress'" ng-class="{ 'status-success': item.Progress == 'Questionnaire Completed', 'status-error': item.Progress == 'Unsent'}">
        {{item.Progress}}
    </td>
    <td ng-if="item.Progress == 'In Progress'" ng-model="progressCalc" class="status-info percent">
        {{item.Qprogress}}
    </td>
    <td width="10%">
        <a href="#/reports/" title{{$index + 1}}="Reports" ng-show="{{item.Progress == 'Questionnaire Completed'}}">
            <span class="stat-icon-report"></span>
        </a>
        <a href="#/sends/{{$index + 1}}" title="Alert" ng-show="{{item.Progress == 'Unsent'}}">
            <span class="stat-icon-bullhorn"></span>
        </a>
        <a href="#/progress/{{$index + 1}}" title="In Progress" ng-show="{{item.Progress == 'In Progress'}}">
            <span class="stat-icon-phone"></span>
        </a>
    </td>
</tr>

还有 JS:

myApp.controller('clientStatus', ['$scope', '$http', function($scope, $http) {
    $http.get('assets/js/lib/angular/clientList.json').success(function(data) {
        $scope.clients = data;

        $scope.progressCalc = function() {
            angular.forEach(function(item) {
                var m = 0.26664;
                var s = 0.26664;
                var i = 0.694375;
                var t = item.Qprogress;
                t = t.replace(/m/g,'');
                t = t.replace(/s/g,'');
                t = t.replace(/i/g,'');
                var ta = t.split("/");
                var tTotal = (ta[0] * m) + (ta[1] * s) + (ta[2] * i);
                tTotal = Math.round(tTotal);
                $('.percent').append(tTotal + '%');
            });
        };
    });
}]);

以及 JSON 格式的片段:

"FirstName": "Bill",
"LastName": "Johnson",
"Company": "Texas Instruments",
"CompanyId": "2345672",
"ClientId": "EFTGE6",
"Title": "CIO",
"Phone": "555-555-5555",
"ClientSystemStatus": "Active",
"CreationDate": "06/03/2015, 10:35:59 am",
"Progress": "In Progress",
"Qprogress": "m125/s40/i0",
"Email": "bjohson@ti.com"

谢谢!

最佳答案

我不知道你到底需要计算什么,但我认为 forEach 不是必需的。所以我做了一些更改/修复,请看一下。

Controller :

$http.get('assets/js/lib/angular/clientList.json').success(function(data) {
    $scope.clients = data;
});

$scope.progressCalc = function(item) {
    var m = 0.26664;
    var s = 0.26664;
    var i = 0.694375;
    var t = item.Qprogress;
    t = t.replace(/m/g,'');
    t = t.replace(/s/g,'');
    t = t.replace(/i/g,'');
    var ta = t.split("/");
    var tTotal = (ta[0] * m) + (ta[1] * s) + (ta[2] * i);
    tTotal = Math.round(tTotal);
    return tTotal + '%';
};

HTML:

注意第一行:我删除了错误的指令调用progressCalc。 下面还有更多内容,作为示例,我调用方法 progressCalc 打印结果。

<tr ng-repeat="item in clients">
    <td>
        <a href="#/details/{{clients.indexOf(item)}}" title="Link to {{item.FirstName}} {{item.LastName}}" class="oly-open">{{item.FirstName}} {{item.LastName}}</a>
    </td>
    <td ng-hide="item.Progress == 'In Progress'" ng-class="{ 'status-success': item.Progress == 'Questionnaire Completed', 'status-error': item.Progress == 'Unsent'}">
        {{item.Progress}}
    </td>
    <td ng-if="item.Progress == 'In Progress'" class="status-info percent">
        {{item.Qprogress}} {{progressCalc(item)}}
    </td>
    <td width="10%">
        <a href="#/reports/" title{{$index + 1}}="Reports" ng-show="{{item.Progress == 'Questionnaire Completed'}}">
            <span class="stat-icon-report"></span>
        </a>
        <a href="#/sends/{{$index + 1}}" title="Alert" ng-show="{{item.Progress == 'Unsent'}}">
            <span class="stat-icon-bullhorn"></span>
        </a>
        <a href="#/progress/{{$index + 1}}" title="In Progress" ng-show="{{item.Progress == 'In Progress'}}">
            <span class="stat-icon-phone"></span>
        </a>
    </td>
</tr>

我没有测试过它,但应该有用或有帮助!

关于javascript - 如何在 ng-repeat 中启动 forEach 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35348149/

相关文章:

javascript - 在 Angular 1 中如何检测是否支持 Angular,如果不支持则在屏幕上显示一条消息

带有嵌套 JSON 数组的 iOS NSPredicate

json 站点地图中的 Javascript 部分通配符匹配

javascript - 在nodejs中将数据发布到另一个页面

javascript - 无法获取哈希密码以使用 bcrypt 保存

javascript - ngrepeat、ngif 和比较日期对象的各个部分

javascript - jquery 数据表从 web API 接收到不正确的 json

javascript - 21 计数游戏(像 nim 游戏)

javascript - 如何水平翻转图像

javascript - 类型错误 : Cannot read property 'username' of undefined from Form Submit