I am new to angular JS and wanted to use that to handle a json passed by ajax. To start with, I have made this testing code to make sure the data can be displayed correctly:
<head>
<script src="path/to/angular.js"></script>
<script>
var app = angular.module('pageApp', []).config(function($interpolateProvider){
$interpolateProvider.startSymbol('<<').endSymbol('>>');
});
</script>
</head>
<body ng-app="pageApp">
<div class="container" ng-controller="tagging">
Text input:<textarea ng-model="description" ng-change="checkTag()" required></textarea><br>
Tag found om database: <span ng-repeat="tag in tags"> << tag >> </span><br>
</div>
</body>
<script>
//$http is not used now but will be used in the actual ajax handling.
function taggingController($scope, $http) {
$scope.tags = [];
$scope.checkTag = function () {
var hardCode = [];
hardCode[1] = "one";
hardCode[3] = "three";
hardCode[5] = "five";
angular.forEach(hardCode, function(value, key) {
console.log($scope.tags[key]);
$scope.tags[key] = value
});
};
};
app.controller("tagging", taggingController);
</script>
However, the code doesn't work and gives the following error:
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: tag in tags, Duplicate key: undefined:undefined, Duplicate value: undefined
When the array is changed to index of consecutive numbers, it works. i.e.
hardCode[1] = "one";
hardCode[2] = "three";
hardCode[3] = "five";
Did I missed anything or Angular JS simply don't have the ability to handle non-consecutive number indexed array?
json_encodedit will look like{3:"three", 5:"five"}. This sort of indexing is not needed much. Far more common to have array of objects like[{id:3,txt: "three"},{id:5,txt:"five"}]