简体   繁体   English

AngularJS - 在ngRepeat中重置ngInit

[英]AngularJS - reset ngInit in ngRepeat

Assume the following JSON structure, which is strongly simplified from what I am currently working with: 假设以下JSON结构,这与我目前正在使用的内容大大简化:

$scope.var1 = {
    "test1": {
        "name": "test1",
        "belongsTo": "var1",
        "additionalInformation": "blabla"
    },
    "test2": {
        "name": "test2",
        "belongsTo": "var1",
        "additionalInformation": "blabla"
    },
    "test3": {
        "name": "test3",
        "belongsTo": "var1",
        "additionalInformation": "blabla"
    }
};

$scope.var2 = {
    "test8": {
        "name": "test8",
        "belongsTo": "var2",
        "additionalInformation": "blabla"
    },
    "test2": {
        "name": "test2",
        "belongsTo": "var2",
        "additionalInformation": "blabla"
    },
    "test9": {
        "name": "test9",
        "belongsTo": "var2",
        "additionalInformation": "blabla"
    }
};

$scope.valuesVar1 = {
    "test1": {
        "value": 1
    },
    "test2": {
        "value": 2
    },
    "test3": {
        "value": 3
    },
};

$scope.valuesVar2 = {
    "test8": {
        "value": 4
    },
    "test2": {
        "value": 5
    },
    "test9": {
        "value": 6
    },
};

Var1 and Var2 represent some data, which always has the same structure. Var1和Var2代表一些数据,它们总是具有相同的结构。 Same for valuesVar1 and valuesVar2 - they have the same structure and contain some additional information for var1 and var2. 对于valuesVar1和valuesVar2也是如此 - 它们具有相同的结构并包含var1和var2的一些附加信息。

I am trying to iterate over var1 and get the value from valuesVar1 using ng-init. 我试图迭代var1并使用ng-init从valuesVar1获取值。 On user interaction the assignment for the iteration is changed to var2, so know we are iterating over var2 and getting the values from valuesVar2. 在用户交互中,迭代的赋值更改为var2,因此我们知道我们正在迭代var2并从valuesVar2获取值。

The problem : When iterating over var2, we should get the value 5 for "test2". 问题 :当迭代var2时,我们应该得到“test2”的值5。 But since ngInit already "knows" test2 from iterating over var1, the value stays 2. 但由于ngInit已经通过迭代var1“知道”test2,因此该值保持为2。

Note : Please don't give answers advising to change the JSON structure or something similar - because I am on my side not able to do this. 注意 :请不要给出答案,建议更改JSON结构或类似的东西 - 因为我不能做到这一点。 I already tried to copy the object like proposed here: angularjs - ngRepeat with ngInit - ngRepeat doesn't refresh rendered value - but this doesn't work. 我已经尝试复制像这里提出的对象: angularjs - ngRepeat with ngInit - ngRepeat不刷新渲染值 - 但这不起作用。 I assume it is because ngInit "remembers" the string value "test2" rather than the object reference (but I don't really know it :) ). 我假设这是因为ngInit“记住”字符串值“test2”而不是对象引用(但我真的不知道:))。

Question : How can I get ngInit to run again on an iteration, where the same key for an object was already used in a previous iteration? 问题 :如何在迭代中再次运行ngInit,其中对象的相同键已在前一次迭代中使用过?

I also prepared a jsfiddle http://jsfiddle.net/S329r/6/ with the information. 我还准备了一个jsfiddle http://jsfiddle.net/S329r/6/ On running it iterates over var1 and outputs the values like this: 在运行时,它迭代var1并输出如下值:

test1: 1
test2: 2
test3: 3

So far so good, I expect this. 到目前为止这么好,我期待这一点。 But when var2 is assigned for the iteration, the following shows: 但是,当为迭代分配var2时,以下显示:

test2: 2
test8: 4
test9: 6

Here I expect test2 to display the value 5. (Hint: In the jsfiddle demo, the button "Change" changes the assignment for the iteration to var2 , "Change Back" changes it back to var1 ). 这里我希望test2显示值5.(提示:在jsfiddle演示中,按钮“Change”将迭代的赋值更改为var2 ,“Change Back”将其更改回var1 )。

One solution is to ensure that you are generating a unique Id for each item in the JSON. 一种解决方案是确保为JSON中的每个项生成唯一的Id。 Of course you can't alter your JSON as you said, but you CAN force angularJS to generate one. 当然你不能像你说的那样改变你的JSON,但你可以强制angularJS生成一个。

Put this in your repeat loop: 把它放在你的重复循环中:

iter in iterateOver track by $id(iter.name + iter.belongsTo)

And as long as that combination of data is unique, your loop will work correctly. 只要数据组合是唯一的,您的循环就能正常工作。

A little more info from the documentation: 从文档中获得更多信息:

For example: item in items track by $id(item). 例如:项目中的项目按$ id(项目)跟踪。 A built in $id() function can be used to assign a unique $$hashKey property to each item in the array. 内置的$ id()函数可用于为数组中的每个项分配唯一的$$ hashKey属性。 This property is then used as a key to associated DOM elements with the corresponding item in the array by identity. 然后,此属性用作关联DOM元素与按标识的数组中相应项的关键字。 Moving the same object in array would move the DOM element in the same way in the DOM. 在数组中移动相同的对象将以相同的方式在DOM中移动DOM元素。

EDIT Another solution is to change your loop a little and don't use ng-init Instead, get your values like this: 编辑另一个解决方案是稍微改变你的循环而不使用ng-init而是获取你的值如下:

<div ng-repeat="iter in iterateOver " >
    {{iter.name}}: {{getValForIter(iter).value}}
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM