當我使用concat方法,我的$範圍不更新
var anotherList = ["2", "3", "4"];
$scope.list = [];
$scope.list.concat(anotherList);
但在循環中使用陣推法,我的$範圍得到更新
當我使用concat方法,我的$範圍不更新
var anotherList = ["2", "3", "4"];
$scope.list = [];
$scope.list.concat(anotherList);
但在循環中使用陣推法,我的$範圍得到更新
你的語法是錯誤的。您需要將返回值.concat()
分配給您的作用域數組。
$scope.list = $scope.list.concat(anotherList);
另外,如果你使用call()
您可以將結果直接綁定到範圍的陣列,而不需要「重新分配」,因爲這將引擎蓋你下進行。
$scope.list.concat.call(anotherList);
大衛說的是正確的。只需添加:concat()不會修改數組,它只是根據收到的參數返回一個新的數組。如果你想保持原來的陣列
,而不是concat
你可以這樣做::選中此獲取更多信息
Array.prototype.push.apply($scope.list, anotherList);
謝謝大衛L,我是小白與JavaScript和AngularJS –
@EricdosReis不是問題!如果你不知道你在找什麼,這很容易忽視。 –