在$scope更新Array内的Array后,我在更新视图时遇到问题。
首先,我检查Array成员是否已经存在:
$scope.myArray = []; if(typeof $scope.myArray[someIndex] == 'undefined') { $scope.myArray[someIndex] = { name: someName, data: [] }; }然后推送到$scope.myArray[someIndex].data :
$scope.myArray[someIndex].data.push(dataContent);此时视图不会更新。
当然,如果我直接推送到$scope.myArray它。 有什么建议么?
编辑: 这里小提琴
I'm having problems updating the view after an Array inside an Array is updated in the $scope.
First i check if the Array member already exists:
$scope.myArray = []; if(typeof $scope.myArray[someIndex] == 'undefined') { $scope.myArray[someIndex] = { name: someName, data: [] }; }Then push to $scope.myArray[someIndex].data:
$scope.myArray[someIndex].data.push(dataContent);At this point the view does not update.
Of course if i directly push to $scope.myArray it does. Any suggestions?
Edit: Fiddle here
最满意答案
它看起来比它简单。
根据这里的响应,我设置了一个允许设置字符串键的关联数组。 如果将数组声明为=[] ,则无法将字符串设置为键。
所以我只是将我的声明$scope.myArray=[]更改$scope.myArray={}并且$scope.myArray={} ,它有效。
It was simpler than it looked.
Based on the response here i am setting an associative array which allows set string keys. If you declare your array as =[] you simply cannot set strings as keys.
So i just changed my declaration $scope.myArray=[] to $scope.myArray={} and voilà, it works.
AngularJS:嵌套数组和视图更新(AngularJS: nested array and view update)在$scope更新Array内的Array后,我在更新视图时遇到问题。
首先,我检查Array成员是否已经存在:
$scope.myArray = []; if(typeof $scope.myArray[someIndex] == 'undefined') { $scope.myArray[someIndex] = { name: someName, data: [] }; }然后推送到$scope.myArray[someIndex].data :
$scope.myArray[someIndex].data.push(dataContent);此时视图不会更新。
当然,如果我直接推送到$scope.myArray它。 有什么建议么?
编辑: 这里小提琴
I'm having problems updating the view after an Array inside an Array is updated in the $scope.
First i check if the Array member already exists:
$scope.myArray = []; if(typeof $scope.myArray[someIndex] == 'undefined') { $scope.myArray[someIndex] = { name: someName, data: [] }; }Then push to $scope.myArray[someIndex].data:
$scope.myArray[someIndex].data.push(dataContent);At this point the view does not update.
Of course if i directly push to $scope.myArray it does. Any suggestions?
Edit: Fiddle here
最满意答案
它看起来比它简单。
根据这里的响应,我设置了一个允许设置字符串键的关联数组。 如果将数组声明为=[] ,则无法将字符串设置为键。
所以我只是将我的声明$scope.myArray=[]更改$scope.myArray={}并且$scope.myArray={} ,它有效。
It was simpler than it looked.
Based on the response here i am setting an associative array which allows set string keys. If you declare your array as =[] you simply cannot set strings as keys.
So i just changed my declaration $scope.myArray=[] to $scope.myArray={} and voilà, it works.
发布评论