在 Angular Js 中动态设置范围对象的属性 [英] Set a property of scope object dynamically in Angular Js
问题描述
我正在编写一个通用方法来从服务中获取一些数据并填充到函数中传递的动态属性名称中.该值确实使用 angular.element 分配分配给文本框,但不会在模型中填充.以下是代码.
<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')">按钮>
这里是我的控制器方法,它在内部使用一个服务来发回一个承诺
$scope.pickImage = function (attrModel) {ImageSelector.selectImage().then(功能(值){//angular.element("#" + attrModel).val(value);$scope[attrModel] = 值;});};
attrModel 是范围对象 contentEntity 中的属性名称,但该属性的名称只能通过方法参数动态获知.
应该可以
I am writing a generic method to get some data from a service and populate in the dynamic property name passed in the function. the value does gets assigned to the text box using angular.element assignment but does not gets populated in the model. following is the code.
<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>
here is my controller method which internally uses a service which sends back a promise
$scope.pickImage = function (attrModel) {
ImageSelector.selectImage().then(
function (value) {
//angular.element("#" + attrModel).val(value);
$scope[attrModel] = value;
});
};
attrModel is a property name in the scope object contentEntity but the name of the property is known ONLY dynamically via the method parameter.
<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;
});
};
should work
这篇关于在 Angular Js 中动态设置范围对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!