javascript 将 $$hashKey 添加到 Angular 中的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23501230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add $$hashKey to object in Angular
提问by Rasmus
The Angular-specific property on enumerated objects $$hashKey
can be used for a lot of things.
枚举对象上的 Angular 特定属性$$hashKey
可用于很多事情。
For example DOM-targeting;
例如 DOM 定位;
<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?
在一些奇怪的情况下,我现在遇到了 $$hashKey 道具尚未设置在我想访问它的对象上,即使它正在用 Angular 重复。有没有办法在初始化对象时自己设置这个属性?
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.
编辑:我的猜测是存在某种形式的执行顺序问题,当 Angular 尚未处理重复时,我访问了该属性。我正在深入观察一个对象,该对象内是一个包含重复对象的数组。它也在我需要访问$$hashKey
属性的那些对象之一上。
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.net/2sbWp/2/
Edit2: jsFiddle http://jsfiddle.net/2sbWp/2/
采纳答案by Marc Kline
Use $timeout with no delay value to defer until the $$hashKey property is available:
使用没有延迟值的 $timeout 推迟到 $$hashKey 属性可用:
$timeout(function(){console.log(lastObj.$$hashKey)});
回答by SoluableNonagon
I created a dummy example based on you code, see here: http://plnkr.co/edit/9PNc6bcy1TO4Zaeyuvwq?p=preview
我根据您的代码创建了一个虚拟示例,请参见此处: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
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
希望这对 $$hashkey 有所了解