javascript - 如何在使用 $index 时动态定义 ng-model

标签 javascript angularjs

我不想使用 $index 来创建一个新的 $scope 变量:

    <div ng-repeat="i in items track by $index">   
            <input autocomplete="off" type="text" 
             ng-model="myVariableName{{$index}}" >
            ....

我不想声明变量:myVariableName0、myVariableName1 等...

最佳答案

在 Controller 中声明空对象

$scope.myVariableName = {};

并在模板中将模型值分配为对象的属性。

ng-model="myVariableName[$index]"

关于javascript - 如何在使用 $index 时动态定义 ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47528443/

相关文章:

javascript - 在 AngularJS 上读取 JSON

javascript - 单击时无法调整循环 "cloud"图像大小

javascript - 将 jquery 与用户脚本结合使用

javascript - Android WebView 和 HTML5 Canvas

javascript - 使用 webpack 观看目录

node.js - 调试proctor(带/不带webstorm)

javascript - Meteor 用户更新时显示提醒

javascript - 在 AngularJS 中添加 ng-switch 后,滚动事件未绑定(bind)到 html 元素

angularjs - 如何使用 Protractor 识别 ionic 标签?

javascript - AngularJS + AngularFire = 如何编辑条目