我想在此代码中向您寻求帮助。
我在控制台中收到错误:
错误:[ngRepeat:dupes]不允许在转发器中重复。使用'跟踪'表达式以指定唯一键。 Repeater:类别中的类别,Duplicate键:string:<,重复值:<
我不知道为什么这段代码不起作用:
category in categories track by $index
就像在这个示例https://docs.angularjs.org/error/ngRepeat/dupes
中一样文件来源:
公开\视图\ categories.html
<h3 class="heading">Category</h3>
<div ng-controller="CategoriesCtrl">
<div class="row">
<div class="categories">
<div ng-repeat="category in categories">
<div class="col-md-6">
<h3>{{category.name}}</h3>
<p>{{category.description}}</p>
<a href="/articles/category/{{category.name}}">Viwe Articels</a>
</div>
</div>
</div>
</div>
</div>
categories.controller.js
angular.module("kB")
.controller('CategoriesCtrl', ['$scope', '$http', function($scope, $http) {
$http.get('/categories').success(function(data) {
$scope.categories = data;
});
}]);
categories.js
var express = require('express');
var router = express.Router();
var Category = require('../models/category');
router.get('/', function(req, res, next) {
Category.getCategories(function(err,categories){
if(err){
console.log(err);
}
res.json(categories);
});
});
router.get('/:id', function(req, res, next) {
Category.getCategoryById(req.params.id, function(err,category){
if(err){
console.log(err);
}
res.json(category);
});
});
module.exports = router;
模型/ category.js
var categorySchema = mongoose.Schema({
name: {
type: String,
index: true,
required: true
},
description: {
type: String
}
});
routis / index.js
var express = require('express');
var router = express.Router();
uter.get('*', function (req, res) {
res.render('index')
})
module.exports = router;
app.js中的
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
类别Obejct
{ getCategories: [Function],
getCategoryById: [Function],
getArticlesByCategory: [Function],
createCategory: [Function] }
答案 0 :(得分:1)
改变
<div ng-repeat="category in categories">
到
<div ng-repeat="category in categories track by $index">
如果你还没有这样做,应该有效。
答案 1 :(得分:0)
现在它适用于:
<div ng-repeat="category in categories track by $index">
如果我有/或*表达:
app.get('/', function(req, res) {
res.render('index')
});
app.get('*', function(req, res) {
res.render('index')
});
但刷新页面后,它变成了一个json文件。