在AngularJS中,如果在嵌套的ng-repeat中发现无法修改模型的问题,可能是因为使用了原始的JavaScript对象作为ng-repeat的迭代器。解决方法是使用AngularJS中的内置方法$index来获取当前迭代的索引,并使用该索引来修改模型。
以下是一个示例代码:
angular.module('myApp', [])
.controller('myController', function($scope) {
$scope.items = [
{
subItems: [
{ name: 'Item 1' },
{ name: 'Item 2' },
{ name: 'Item 3' }
]
},
{
subItems: [
{ name: 'Item 4' },
{ name: 'Item 5' },
{ name: 'Item 6' }
]
}
];
$scope.updateModel = function(parentIndex, index) {
console.log(parentIndex, index);
// 在这里可以修改模型
// 例如:$scope.items[parentIndex].subItems[index].name = 'New Name';
};
});
在上面的示例中,我们使用了$parent.$index来获取父级ng-repeat的索引,$index来获取当前ng-repeat的索引。通过将这些索引传递给updateModel函数,我们可以在函数中修改模型。
请注意,在实际应用中,你可能需要根据具体的业务逻辑进行适当的修改。