gpt4 book ai didi

javascript - Angularjs 更新指令 View - 错误 : ngRepeat:dupes

转载 作者:行者123 更新时间:2023-11-28 04:44:40 24 4
gpt4 key购买 nike

index.html:

<body ng-app="homeApp" ng-controller="homeCtrl">
<xi-folder-nav></xi-folder-nav>
<button class="btn btn-default " type="button" xi-search>Search</button>
</body>

nav.html:

<ol class="breadcrumb" >
<li ng-repeat="n in folderNav" ng-class="{'active' : $last}">
<a href="#" ng-hide="$last">{{ n }}</a>
<span ng-show="$last">{{ n }}</span>
</li>
</ol>

service.js:

angular
.module('homeApp')
.service('dataService', dataService);

dataService.$inject = ['$rootScope'];
function dataService($rootScope) {
var service = {
folderNav: ['root'],

addFolderNav: function(nav){
service.folderNav.push( nav );
$rootScope.$broadcast( 'folderNav.update' );
},
};

return service;
}

controller.js:

angular
.module('homeApp')
.controller('homeCtrl',homeCtrl);

homeCtrl.$inject = ['$scope','dataService'];
function homeCtrl($scope,dataService) {
$scope.$on( 'folderNav.update', function( event ) {
$scope.folderNav = dataService.folderNav;
$scope.$apply();
});
$scope.folderNav = dataService.folderNav;

directive.js:

angular
.module('homeApp')
.directive('xiFolderNav', xiFolderNav);
.directive('xiSearch', xiSearch);

xiFolderNav.$inject = ['dataService'];
function xiFolderNav(dataService) {
return{
restrict: 'E',
templateUrl: 'js/app/home/nav.html',
};
}

xiSearch.$inject = ['dataService'];
function xiSearch(dataService) {
return{
restrict: 'A',
link: link
};

function link(scope, element, attr) {
element.bind('click', function(){
dataService.addFolderNav('gg');
});

}
}

我想向 nav.html 添加一些数据并更新 View 。但是当我第二次单击按钮时,它显示一些错误。

我已经像大多数人一样将 $scope.$apply() 添加到 Controller 中,但效果不佳!

错误:

Error: [ngRepeat:dupes] http://errors.angularjs.org/1.5.11/ngRepeat/dupes?p0=n%20in%20folderNav&p1=string%3Agg&p2=gg

最佳答案

此错误是由ng-repeatRepeater中的重复键引起的。请参阅here .

您可以通过 $index 添加track以避免此错误,或者在推送到 Repeater 之前检查重复项。

关于javascript - Angularjs 更新指令 View - 错误 : ngRepeat:dupes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43485728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com