The Angular-specific property on enumerated objects $$hashKey
can be used for a lot of things.
For example DOM-targeting;
<div ng-repeat="obj in objects">
<label for="field-{{obj.$$hashKey}}">
Label
</label>
<input type="text" id="field-{{obj.$$hashKey}}" />
</div>
In some weird case I am experiencing now the $$hashKey prop is not yet set on a object I want to access it on even though it is being repeated with Angular. Is there a way to set this property yourself when initializing the object?
Edit: My guess is that there is some form of execution order issue, that I access the property when Angular has yet to process the repetition.
I am deep watching an object, within that object is an array with objects which is getting repeated. It's also on one of those objects that I need to access the $$hashKey
property on.
Simple example;
var MyController = function($scope, Obj)
{
$scope.obj = {
list: [obj, obj, obj, obj]
};
$scope.$watch("obj", function()
{
var lastObj = $scope.obj.list[$scope.obj.list.length - 1];
console.log(lastObj.$$hashKey); // Undefined?
}, true);
$scope.addObj = function()
{
$scope.obj.list.push(new Obj());
};
};
Edit2: jsFiddle /
The Angular-specific property on enumerated objects $$hashKey
can be used for a lot of things.
For example DOM-targeting;
<div ng-repeat="obj in objects">
<label for="field-{{obj.$$hashKey}}">
Label
</label>
<input type="text" id="field-{{obj.$$hashKey}}" />
</div>
In some weird case I am experiencing now the $$hashKey prop is not yet set on a object I want to access it on even though it is being repeated with Angular. Is there a way to set this property yourself when initializing the object?
Edit: My guess is that there is some form of execution order issue, that I access the property when Angular has yet to process the repetition.
I am deep watching an object, within that object is an array with objects which is getting repeated. It's also on one of those objects that I need to access the $$hashKey
property on.
Simple example;
var MyController = function($scope, Obj)
{
$scope.obj = {
list: [obj, obj, obj, obj]
};
$scope.$watch("obj", function()
{
var lastObj = $scope.obj.list[$scope.obj.list.length - 1];
console.log(lastObj.$$hashKey); // Undefined?
}, true);
$scope.addObj = function()
{
$scope.obj.list.push(new Obj());
};
};
Edit2: jsFiddle http://jsfiddle/2sbWp/2/
Use $timeout with no delay value to defer until the $$hashKey property is available:
$timeout(function(){console.log(lastObj.$$hashKey)});
A working fork of your Fiddle
I created a dummy example based on you code, see here: http://plnkr.co/edit/9PNc6bcy1TO4Zaeyuvwq?p=preview
var app = angular.module( 'gssApp', [] );
app.controller( 'gssAppController', [ '$scope', '$http', function ( $scope, $http )
{
var obj = {'hash': 'test'};
$scope.newObject = {'hash': 'test'};
$scope.model = null;
$scope.objects = [];
$scope.addObj = function(){
if($scope.model !== null && $scope.model !== undefined){
$scope.objects.push({'hash': $scope.model});
$scope.model = '';
angular.forEach($scope.objects, function(key, value){
console.log(value, key);
});
}
};
}] );
HTML
<body ng-app="gssApp" ng-controller="gssAppController">
<div ng-repeat="obj in objects">
<label for="field-{{obj.$$hashKey}}" ng-click="alert(obj.$$hashKey)">
Label
</label>
<input type="text" id="field-{{obj.$$hashKey}}" />
</div>
<input type="text" placeholder='add new key' ng-model="model">
<button ng-click="addObj();">Submit</button>
</body>
Hopefully this sheds some light on the $$hashkey