Angular ng-repeat不适用于动态检索的数据

时间:2015-12-01 02:52:08

标签: angularjs

我使用Angular使用$ http获取数据并面临一个奇怪的问题。在下面的代码中,我有两个变量,counter&硬编码。它们都具有可以在这里看到的确切数据......唯一的区别是Counter是使用$ http调用动态检索的,而HardCoded是直接声明的。

Counter - [{"name":"Cores Used","percent":"46"},{"name":"Drive Space Used","percent":"66"},{"name":"Memory Used","percent":"30"},{"name":"Network Capacity Used","percent":"52"},{"name":"CPU usage overall","percent":"54"}] 

HardCoded - [{"name":"Cores Used","percent":"59"},{"name":"Drive Space Used","percent":"40"},{"name":"Memory Used","percent":"63"},{"name":"Network Capacity Used","percent":"64"},{"name":"CPU usage overall","percent":"49"}] 

.directive("sfBar", function () {
    var linkFunction = function (scope, element, attributes) {
        scope.counters = attributes["counters"];
        scope.hardcoded = [
            {"name": "Cores Used", "percent": "59"},
            {"name": "Drive Space Used", "percent": "40"},
            {"name": "Memory Used", "percent": "63"},
            {"name": "Network Capacity Used", "percent": "64"},
            {"name": "CPU usage overall", "percent": "49"}
        ];
    };
    return {
        restrict: 'E',
        templateUrl: "app/common/services/controls/bar.html",
        link: linkFunction,
        scope: {
            counters: '@'
        }
    };
})

当我尝试对硬编码执行ng-repeat时,它按预期工作。但是,当我为计数器尝试相同时,它错误地说......

  

错误:[ngRepeat:dupes]   http://errors.angularjs.org/1.3.20/ngRepeat/dupes?p0=counter%20in%20counters&p1=string%3A%22&p2=%22       在错误(本机)       在http://localhost:2000/app/common/vendors/angular/angular.min.js:6:417       在http://localhost:2000/app/common/vendors/angular/angular.min.js:235:172       在Object.fn(http://localhost:2000/app/common/vendors/angular/angular.min.js:123:30)       at n。$ digest(http://localhost:2000/app/common/vendors/angular/angular.min.js:124:114)       at n。$ apply(http://localhost:2000/app/common/vendors/angular/angular.min.js:127:12)       在l(http://localhost:2000/app/common/vendors/angular/angular.min.js:81:195)       在F(http://localhost:2000/app/common/vendors/angular/angular.min.js:85:314)       在XMLHttpRequest.C.onload(http://localhost:2000/app/common/vendors/angular/angular.min.js:86:349

HTML

Counter - {{counters}} <br><br>
        HardCoded - {{hardcoded}} <br><br>
        <div ng-repeat="hc in hardcoded">
            {{hc}}
        </div><br><br>
        <div ng-repeat="counter in counters">
            {{counter}}
        </div><br><br>

我很挣扎,并会感激任何帮助或指针。

0 个答案:

没有答案