0

Is it possible to add the element id in Angular,

<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>

I wonder if is it possible to add id at isEditable(id)", I mean the id shall be the element.id?

3
  • Do you mean that id would be "ruleResetType"? Commented May 23, 2014 at 1:21
  • It's possible, but it's hard for a reason: because using id's is not good Angular practise. It couples your view to your controller tightly. Instead, use some property on the scope, or even just ng-disabled="isEditable('ruleResetType')" in the markup. Commented May 23, 2014 at 1:23
  • yes, but I want not to write the id, I looking for something programmatically Commented May 23, 2014 at 10:26

1 Answer 1

1

This should work, but like Greg says, you probably don't want to use the element 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);
            }
        }
    };
});

Then (assuming the existence of disabledElements on your scope and elements are disabled by default) add this attribute to your HTML elements:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.