将作用域变量设置为ng repeat中的ng模型

Setting scope variable to ng-model in ng-repeat

本文关键字:ng repeat 模型 中的 作用域 变量 设置      更新时间:2023-09-26

这是我的代码

HTML

 <ul>
    <li ng-repeat="obj in objsInObj">
        obj.str = <input ng-model="obj.str" />
    </li>
 </ul>
 obj in <code>{{ strA.a1 }}</code>
 obj in <code>{{ strA.a2 }}</code>

Javascript

$scope.strA = {a1:"abc", a2:"bcd"};
$scope.objsInObj = {
    a: {
        str: $scope.strA.a1
    },
    b: {
        str: $scope.strA.a2
    }
};

我想将$scope.strA.a1和$scope.trA.a2设置为两个文本框的ng模型,在这里,最初文本框值加载正确,但如果我更改范围变量中不可见的文本框内的值

我不明白为什么你需要这个结构,但这个代码可以很好地工作

<ul>
    <li ng-repeat="obj in objsInObj track by $index">
        obj.str = <input ng-model="obj.str" data-ng-change="Modify($index, obj);" />
    </li>
</ul>

这是javascript

$scope.Modify = function(index, v) {
    $scope.strA["a"+ (index+1)] = v.str;
};

这里是工作小提琴