0

I wonder if there is a way in angularjs to add or remove attributes according to condition. for example, instead of writing something like this

<div ng-if="isClickable" ng-click="clicked()">This is my button</div> <div ng-if="!isClickable"> this is my button</div> 

I will be able to write something, maybe, like this:

<div ng-click="clicked()" ng-attrs="{'ng-click': isClickable}"> 
2
  • 3
    Why don't you just use <div ng-click="isClickable && clicked()">This is my button</div> Commented Jul 30, 2017 at 18:56
  • 1
    Adding attributes which are AngularJS directives is problematic as those added directives need to be compiled. Removing those directives is even more problematic. The framework does not support such. Look for another way to achieve what you want. Commented Jul 30, 2017 at 20:39

1 Answer 1

2

You can't remove attribute using any angular directive (you can do this via writing your own custom directive). Rather you can convert such div's into button and use ng-disabled

<button ng-disabled="!isClickable" ng-click="clicked()">This is my button</button> 

Or you can use way suggested by @JBNizet

<div ng-click="isClickable && clicked()">This is my button</div> 
Sign up to request clarification or add additional context in comments.

2 Comments

This is simple in this scenario , but in complex scenario its easier to duplicate
@user3554268 can you explain what complex scenario you have?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.