在Angular Js中动态设置范围对象的属性 [英] Set a property of scope object dynamically in Angular Js

查看:106
本文介绍了在Angular Js中动态设置范围对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通用方法来从服务中获取一些数据并填充函数中传递的动态属性名称。使用angular.element赋值将值分配给文本框,但不会在模型中填充。以下是代码。

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是范围对象contentEntity中的属性名称,但该属性的名称仅通过动态通知方法参数。

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;
        });
};

应该有效

这篇关于在Angular Js中动态设置范围对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆