的index.html:Angularjs更新指令視圖 - 錯誤:ngRepeat:愚弄
<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
和更新視圖。但是當我第二次點擊按鈕時,它會顯示一些錯誤。
我已經像大多數人一樣將$scope.$apply()
添加到控制器中,但效果不佳!
錯誤:
Error: [ngRepeat:dupes] http://errors.angularjs.org/1.5.11/ngRepeat/dupes?p0=n%20in%20folderNav&p1=string%3Agg&p2=gg
有關錯誤的信息,請參閱[AngularJS錯誤參考 - ngRepeat:dupes](http://errors.angularjs.org/1.5.11/ngRepeat/dupes?p0=n%20in%20folderNav&p1=string%3Agg&p2= gg) – georgeawg