在Angularjs中从Controller获取元素id

Getting element id from Controller in Angularjs

本文关键字:元素 id 获取 Controller Angularjs 中从      更新时间:2023-09-26

是否可以在Angular中添加元素id,

<div class="col-sm-8">
                <select class="form-control input-sm"
                        id="ruleResetType"
                        name="ruleResetType"
                        ng-model="rule.resetType"
                        ng-options="data as data for data in Type"
                        ng-required="true"
                        ng-disabled="isEditable(id)">
                </select>
</div>

我想知道是否可以在isEditable(id)中添加id,我的意思是id应该是元素。id?

这应该可以工作,但是就像Greg说的,您可能不想使用元素id。

app.directive("disableIfIn", function(){
    return {
        restrict: 'A',
        transclude: true,
        scope: {
            disabledElements: '@disableIfIn'
        },
        template: '<div ng-transclude></div>',
        link: function (scope, element, attrs) {
            if(scope.disabledElements.indexOf(attrs.id) === -1){
                element.prop('disabled', false);
            }
        }
    };
});

然后(假设在你的作用域中存在disabledElements并且元素默认是禁用的)将这个属性添加到你的HTML元素中:

disable-if-in="{{disabledElements}}"