Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 将一个作用域分配给另一个作用域_Angularjs_Scope_Angularjs Scope - Fatal编程技术网

Angularjs 将一个作用域分配给另一个作用域

Angularjs 将一个作用域分配给另一个作用域,angularjs,scope,angularjs-scope,Angularjs,Scope,Angularjs Scope,我不知道为什么我将一个作用域分配给另一个作用域,如下所示: $scope.test1 = "hello"; $scope.test1 = $scope.test2; 有时如果我更改$scope.test2,$scope.test1也会更改,有时不会。 有时,如果我更改$scope.test1,$scope.test2也会更改 为什么会这样?也许我是在承诺什么的 我不需要一个解决方案,我只需要这个理论,angular.copy就是解决方案 这是我的密码: $scope.topicsQuery.s

我不知道为什么我将一个作用域分配给另一个作用域,如下所示:

$scope.test1 = "hello";
$scope.test1 = $scope.test2;
有时如果我更改$scope.test2,$scope.test1也会更改,有时不会。 有时,如果我更改$scope.test1,$scope.test2也会更改

为什么会这样?也许我是在承诺什么的

我不需要一个解决方案,我只需要这个理论,angular.copy就是解决方案

这是我的密码:

$scope.topicsQuery.should = $scope.appliedOrFilters;
$scope.appliedOrFilters.splice(i, 1); 
$scope.topicsQuery.should.splice(filtersToDelete[i],1);
如果appliedOrFilters更改,则topicsQuery.should也将更改,如果topicsQuery.should更改,则appliedOrFilters也将更改

但如果我说:

$scope.topicsQuery.should = [];
没有什么变化

.splice是否更改对象的属性

原始答案 从

  • Javascript总是按值传递,但当变量引用 对象(包括数组),“值”是对该对象的引用

  • 更改变量的值永远不会更改基础变量 原语或对象,它只是将变量指向新的原语或对象 反对

  • 但是,更改对象引用的对象的属性 变量确实会更改基础对象
因此,如果更改对象引用,它将不会被更新

$scope.test1 = "hello";
$scope.test2 = $scope.test1;
$scope.test1 = "world";

console.log($scope.test2); //prints "hello"
但是,如果更改对象的属性,它将被更新:

$scope.test1 = {"title": "hello"};
$scope.test2 = $scope.test1;
$scope.test1.title = "world";

console.log($scope.test2.title); //prints "world"
-

有关最新问题 对数组
的所有修改应
.appliedOrFilters
将在2个变量上看到

$scope.appliedOrFilters.splice(i, 1);
$scope.topicsQuery.should.splice(filtersToDelete[i],1);
如中所示,splice编辑阵列本身,因此效果良好

$scope.topicsQuery.should=[]

在这里创建一个新数组并将其分配给
。应该

因此,
.appliedOrFilters
仍然指向上一个数组,这是完全正常的

如果要擦除数组,只需执行以下操作

$scope.topicsQuery.should.splice(0, $scope.topicsQuery.should.length);

谢谢你,伙计,你能检查一下我最近的更新吗,我将非常感激
$scope.topicsQuery.should.splice(0, $scope.topicsQuery.should.length);