我使用 Angular 从 $http
调用返回了一个嵌套很深的 JSON 对象。在我的模板中,我必须嵌套一些 ng-repeats
才能显示数据。我似乎无法弄清楚如何在文本输入上使用 ng-model
绑定(bind)数据。
我读了this question其中表示返回对象不是 $scope
中的自动数据,您必须遍历数据并实例化结构。我也尝试过同样的结果。
// Seemingly unnecessary code
angular.forEach(Object.keys($scope.sources), function(sourcename){
$scope.sourceData[sourcename] = {};
angular.forEach(Object.keys($scope.sources[sourcename]), function(key){
$scope.sourceData[sourcename][key] = $scope.sources[sourcename][key];
});
这是一个显示我尝试的 fiddle :
我只想将值填充到字段中并绑定(bind)到模型。在此先感谢您的任何建议。
最佳答案
您示例中的 HTML 有点不对。
这是一个working fiddle .
“不工作”input
只是在 ng-model
中有一些代码不工作。
首先,您不需要在 Angular 指令属性中插入 {{ }}
。因此,这包括 ng-model
。所以 {{key}}
不是必需的。
此外,sourceData
拼写错误。它应该是 sourcedata
并且大小写很重要。
所以 ng-model
的最终结果是 ng-model="sourcedata[key]"
:
<li ng-repeat="(key,value) in sourcedata">
WORKS: <input type="text" value="{{value}}" /><br/>
DOESN'T: <input type="text" ng-model="sourcedata[key]" />
</li>
关于javascript - AngularJS:如何在嵌套的 ng-repeats 中绑定(bind)数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22430753/