gpt4 book ai didi

javascript - md-tabs 在其重复对象发生更改时失去范围 [AngularJS Material ]

转载 作者:数据小太阳 更新时间:2023-10-29 05:28:38 25 4
gpt4 key购买 nike

请看这个codepen

只要您单击 UseDummy2 btn,它只会更改 md-tabs 重复的变量,我就会松开 $scope.selectedIndex 值。 $scope.selectedIndex 重置为 0,第一个选项卡被选中。

如何在更改 $scope.lineDirections 后保持选中的选项卡?

我试过使用 $rootScope.selectedIndex 但还是不行。

最佳答案

ng-tab 有数组“lineDirections”的观察者,在这个观察者中,他们正在重置 attr md-selected(“selectedIndex”)的值,您可以使用闭包来维护它,如下所示:

 $scope.useDummyArray1 = function () {
var selectedIndex = $scope.selectedIndex;
$timeout(function(){
$scope.selectedIndex = selectedIndex;
});
$scope.lineDirections = $scope.dummyArray1;
};

在所有三个函数中执行相同的操作。超时代码将在 watcher 之后触发,这将再次设置“selectedIndex”的先前值

你也可以用下面的方式(但不应该用这种方式,只是为了验证)

$scope.useDummyArray1 = function () {
for(var i=0;i<$scope.lineDirections.length;i++){
$scope.dummyArray1[i].$$hashKey = $scope.lineDirections[i].$$hashKey;
}
$scope.lineDirections = $scope.dummyArray1;
};

这将保持 $$hashKey 的值不变,这将防止 angular 触发观察者。但不推荐,原因如下:1) $$hashKey 由 angular 内部使用,玩这些键不是一个好主意。2) 如果您的“lineDirections”长度不同,那么某些 $$hashKey 会发生变化。

关于javascript - md-tabs 在其重复对象发生更改时失去范围 [AngularJS Material ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44739640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com