2016-01-22 42 views
2

當我使用concat方法,我的$範圍不更新

var anotherList = ["2", "3", "4"]; 
$scope.list  = []; 

$scope.list.concat(anotherList); 

但在循環中使用陣推法,我的$範圍得到更新

回答

5

你的語法是錯誤的。您需要將返回值.concat()分配給您的作用域數組。

$scope.list = $scope.list.concat(anotherList); 

另外,如果你使用call()您可以將結果直接綁定到範圍的陣列,而不需要「重新分配」,因爲這將引擎蓋你下進行。

$scope.list.concat.call(anotherList); 

查看documentation

+0

謝謝大衛L,我是小白與JavaScript和AngularJS –

+0

@EricdosReis不是問題!如果你不知道你在找什麼,這很容易忽視。 –

0

,而不是concat你可以這樣做::選中此獲取更多信息

Array.prototype.push.apply($scope.list, anotherList); 
相關問題