Javascript 在Angular Js中动态设置范围对象的属性
我正在编写一个通用方法,从服务中获取一些数据,并填充函数中传递的动态属性名。使用angular.element赋值将值指定给文本框,但不会在模型中填充。下面是代码Javascript 在Angular Js中动态设置范围对象的属性,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我正在编写一个通用方法,从服务中获取一些数据,并填充函数中传递的动态属性名。使用angular.element赋值将值指定给文本框,但不会在模型中填充。下面是代码 <div class="input-group"> <input class="form-control" id="ImgRollover" name="ImgRollover" ng-model="contentEntity.imgRollover" placeholder="" readonly="readonl
<div class="input-group">
<input class="form-control" id="ImgRollover" name="ImgRollover" ng-model="contentEntity.imgRollover" placeholder="" readonly="readonly" type="text">
<div class="input-group-btn">
<button class="btn" type="button" ng-click="pickImage('contentEntity.imgRollover')">
</button>
</div>
attrModel是范围对象contentEntity中的属性名称,但该属性的名称只能通过方法参数动态知道
<button class="btn" type="button" ng-click="pickImage('contentEntity', 'imgRollover')"></button>
$scope.pickImage = function (attrModel1, attrModel2) {
ImageSelector.selectImage().then(function (value) {
$scope.[attrModel1][attrModel2] = value;
});
};
$scope.pickImage=函数(attrModel1、attrModel2){
ImageSelector.selectImage()。然后(函数(值){
$scope.[attrModel1][attrModel2]=值;
});
};
应该有效
$scope.pickImage=函数(attrModel1、attrModel2){
ImageSelector.selectImage()。然后(函数(值){
$scope.[attrModel1][attrModel2]=值;
});
};
应该可以已经有了答案,但是,就像发布一些关于动态属性的内容一样
<!DOCTYPE html>
<html>
<head>
<script src="https://code.angularjs.org/1.2.23/angular.js"></script>
<script type="text/javascript">
var value = 0;
function mainCtrl($scope) {
value++;
$scope.pickImage = function (attrModel) {
value++;
alert(attrModel)
$scope[attrModel] = value;
};
$scope.getValue = function(attrModel) {
return $scope[attrModel];
}
}
</script>
</head>
<body ng-app ng-controller="mainCtrl">
<input type="text" ng-model="test.obj" />
<br/>
<button ng-click="pickImage(test.obj)">test</button>
<br/>
display the value afoter button click,
note there is no single quote
<br/>
value: {{ getValue(test.obj) }}
</body>
</html>
var值=0;
函数mainCtrl($scope){
值++;
$scope.pickImage=函数(attrModel){
值++;
警报(属性模型)
$scope[attrModel]=值;
};
$scope.getValue=函数(attrModel){
返回$scope[attrModel];
}
}
测试
单击按钮后显示值,
注:没有单一报价
值:{{getValue(test.obj)}
已经有了答案,但我想发布一些关于动态属性的内容
<!DOCTYPE html>
<html>
<head>
<script src="https://code.angularjs.org/1.2.23/angular.js"></script>
<script type="text/javascript">
var value = 0;
function mainCtrl($scope) {
value++;
$scope.pickImage = function (attrModel) {
value++;
alert(attrModel)
$scope[attrModel] = value;
};
$scope.getValue = function(attrModel) {
return $scope[attrModel];
}
}
</script>
</head>
<body ng-app ng-controller="mainCtrl">
<input type="text" ng-model="test.obj" />
<br/>
<button ng-click="pickImage(test.obj)">test</button>
<br/>
display the value afoter button click,
note there is no single quote
<br/>
value: {{ getValue(test.obj) }}
</body>
</html>
var值=0;
函数mainCtrl($scope){
值++;
$scope.pickImage=函数(attrModel){
值++;
警报(属性模型)
$scope[attrModel]=值;
};
$scope.getValue=函数(attrModel){
返回$scope[attrModel];
}
}
测试
单击按钮后显示值,
注:没有单一报价
值:{{getValue(test.obj)}
我知道这已经得到了很好的回答,但我想成为一个动态属性创建者。
它通过'拆分attrModel
。
然后编辑$scope
并添加和/或返回每个属性。如果每个属性已经存在或不存在,我们将保留while循环外的最后一个键,以便只需将值附加到它
$scope.pickImage = function (attrModel) {
ImageSelector.selectImage().then(
function (value) {
var parent = $scope,
current,
attribute,
attributes = attrModel.split('.');
while(attributes.length > 1 &&
(attribute = attributes.shift()) &&
(current = parent[attribute] || (parent[attribute] = {}))) {
parent = current;
}
current[attributes[0]] = value;
});
};
当然,如果你想用一种角度的方式来做,你必须使用一个服务来做,它可能看起来像这样
我知道这已经得到了很好的回答,但我想成为一个动态的财产创造者。 它通过
'拆分attrModel
。
然后编辑$scope
并添加和/或返回每个属性。如果每个属性已经存在或不存在,我们将保留while循环外的最后一个键,以便只需将值附加到它
$scope.pickImage = function (attrModel) {
ImageSelector.selectImage().then(
function (value) {
var parent = $scope,
current,
attribute,
attributes = attrModel.split('.');
while(attributes.length > 1 &&
(attribute = attributes.shift()) &&
(current = parent[attribute] || (parent[attribute] = {}))) {
parent = current;
}
current[attributes[0]] = value;
});
};
当然,如果你想用一种角度的方式来做,你必须使用一个服务来做,它可能看起来像这样
ng_模型-是打字错误吗?必须是ng model使用
ng model
绑定时,无需使用val()
设置值(使用正确的语法)。您正在通过id“contentEntity.imgRollover”选择一个不存在的元素,因此请添加两个参数。。。pickImage('contentEntity','imgRollover')@Vikram如果需要使用未知的n对象深度模型,我还提出了其他建议-是打字错误吗?必须是ng model使用ng model
绑定时,无需使用val()
设置值(使用正确的语法)。您正在通过id“contentEntity.imgRollover”选择一个不存在的元素,因此请添加两个参数。。。pickImage('contentEntity','imgRollover')@Vikram如果需要使用未知的对象深度,我还提出了其他建议