在 angular 中按 JSON 中的值传递
Pass by Value in JSON in angular
我在 JSON、
中观察到一个奇怪的行为
让我试着描述一下场景,我们有一个 angular 范围变量说
$scope.regValue = 0;
这在控制器中初始化,并在 HTML 模板 中的 ngModel 下使用以跟踪值的变化。
现在让我们将其分配给 JSON 对象键,
var jsonObj = {
'key1': $scope.regValue
};
我观察到的是,现在
jsonObj['key1']
值将为 0,即 正确。
但是当ngModel改变$scope.regValue的值时,在JSON键下没有体现值,这意味着
jsonObj['key1']
仍然等于0.
虽然 $scope.regValue 已经改变了。
这种行为是预期的吗,或者通过引用传递不能将它自己的引用传递给另一个变量?
对象和原始值(字符串、数字、布尔值)之间存在 JavaScript 差异。基元永远不会通过引用传递,而对象总是。
因此,对于您的对象 属性,您正在为
分配一个数值
var jsonObj = {
'key1': $scope.regValue // this is a number and assigned as such
};
当您更新 $scope
对象上的 属性 时,您并没有修改 jsonObj
变量的值。
var $scope = {
regValue: 0
}
console.log($scope);
console.log($scope.regValue);
var jsonObj = {
key1: $scope.regValue
}
console.log(jsonObj);
console.log(jsonObj.key1);
$scope.regValue = 5;
console.log($scope);
console.log($scope.regValue);
console.log(jsonObj);
console.log(jsonObj.key1);
试试这个进行比较:
var otherObject = $scope;
你会看到otherObject
反映了$scope
的所有变化,因为它实际上是在引用内存中的同一个对象
var $scope = {
regValue: 0
}
console.log($scope);
var otherObject = $scope;
$scope.regValue = 5;
console.log($scope);
console.log(otherObject);
如果你想完成 'second approach',请使用观察者:
$scope.$watch('regValue', function (newValue, oldValue, scope) {
jsonObj['key1'] = newValue ;
});
通过引用传递值的示例
var jsonObj = {
'key1': $scope.regValue
};
// This function modifies the passed object.
var modify(obj) {
obj['key1'] = 12122;
};
modify(jsonObj); //jsonObj has a new value because was modified.
例如,如果模型 ($scope.regValue
) 附加到 input-text
并且用户输入新值,$digest
的进程将处理该信息并分配该数据到特定模型 $scope.regValue
。因此,对象 jsonObj
未被修改,因为从未被 $digest
.
处理过
如果您希望属性 key1
在任何进程修改 $scope.regValue
时被修改(即:用户输入),请使用 watchers
.
资源
我在 JSON、
中观察到一个奇怪的行为让我试着描述一下场景,我们有一个 angular 范围变量说
$scope.regValue = 0;
这在控制器中初始化,并在 HTML 模板 中的 ngModel 下使用以跟踪值的变化。
现在让我们将其分配给 JSON 对象键,
var jsonObj = {
'key1': $scope.regValue
};
我观察到的是,现在
jsonObj['key1']
值将为 0,即 正确。
但是当ngModel改变$scope.regValue的值时,在JSON键下没有体现值,这意味着
jsonObj['key1']
仍然等于0.
虽然 $scope.regValue 已经改变了。
这种行为是预期的吗,或者通过引用传递不能将它自己的引用传递给另一个变量?
对象和原始值(字符串、数字、布尔值)之间存在 JavaScript 差异。基元永远不会通过引用传递,而对象总是。
因此,对于您的对象 属性,您正在为
分配一个数值var jsonObj = {
'key1': $scope.regValue // this is a number and assigned as such
};
当您更新 $scope
对象上的 属性 时,您并没有修改 jsonObj
变量的值。
var $scope = {
regValue: 0
}
console.log($scope);
console.log($scope.regValue);
var jsonObj = {
key1: $scope.regValue
}
console.log(jsonObj);
console.log(jsonObj.key1);
$scope.regValue = 5;
console.log($scope);
console.log($scope.regValue);
console.log(jsonObj);
console.log(jsonObj.key1);
试试这个进行比较:
var otherObject = $scope;
你会看到otherObject
反映了$scope
的所有变化,因为它实际上是在引用内存中的同一个对象
var $scope = {
regValue: 0
}
console.log($scope);
var otherObject = $scope;
$scope.regValue = 5;
console.log($scope);
console.log(otherObject);
如果你想完成 'second approach',请使用观察者:
$scope.$watch('regValue', function (newValue, oldValue, scope) {
jsonObj['key1'] = newValue ;
});
通过引用传递值的示例
var jsonObj = {
'key1': $scope.regValue
};
// This function modifies the passed object.
var modify(obj) {
obj['key1'] = 12122;
};
modify(jsonObj); //jsonObj has a new value because was modified.
例如,如果模型 ($scope.regValue
) 附加到 input-text
并且用户输入新值,$digest
的进程将处理该信息并分配该数据到特定模型 $scope.regValue
。因此,对象 jsonObj
未被修改,因为从未被 $digest
.
如果您希望属性 key1
在任何进程修改 $scope.regValue
时被修改(即:用户输入),请使用 watchers
.