ngRepeat:dupes使用名为“name”的数组在Repeater中复制键。使用其他名称它可以正常工作

时间:2016-03-10 03:52:49

标签: javascript angularjs angularjs-ng-repeat ng-repeat

问题是,当变量名为'name'时,出现错误'dupes Duplicate Key in Repeater'!

但是,为什么!?这是代码:

app.js

var galleryModule = angular.module ("gallery",[]);
var name = ['Juan Manuel Jimenez','Carlos ALberto','Choko Barrios','Cota','Huevo'];

galleryModule.controller("appController", ["$scope", function($scope) {
        $scope.names = name;
}]);  

index.html

<!DOCTYPE html>
<html lang="en" ng-app="gallery">
<head>
    <title>
        GALERIA
    </title>
    <meta charset="utf-8">
    <script src="JS/angular.js"></script>
    <script src="JS/app.js"></script>
</head>
<body>
    <div class="container" ng-controller="appController">
     <div ng-repeat="n in names">
        Bienvenido: {{n}}
     </div>
    </div>
</body>
</html>  

但是,如果您通过某事以及appController内部更改 app.js 名称的第二行,就会出现这种情况。 。
代码将工作!那对我来说很奇怪。

更新
我已经解决了我的问题。我在全局范围内使用 name varible,所以我遇到了windows.name

的麻烦

然后,我选择的解决方案是使用 clousure ,如:

(function() {
    var galleryModule = angular.module ("gallery",[]);
    var name = ['Juan Manuel Jimenez','Carlos ALberto','Choko Barrios','Cota','Huevo'];

    galleryModule.controller("appController", ["$scope", function($scope) {
            $scope.names = name;
    }]);
})();

谢谢大家! :)

1 个答案:

答案 0 :(得分:0)

亲爱的

我认为您应该在控制器范围内定义名称数组。而你将摆脱这个问题。还有一件事你应该在$ scope中做一些改变。仅将其定义为功能参数。尽可能使用最少的[]括号。

var galleryModule = angular.module ("gallery",[]);

galleryModule.controller("appController", function($scope) {
     var name = ['Juan Manuel Jimenez','Carlos ALberto','Choko Barrios','Cota','Huevo'];
        $scope.names = name;
});

谢谢&amp;干杯