-
Notifications
You must be signed in to change notification settings - Fork 27.5k
feat(ngAttr): implement conditional/interpolated attributes directive #4269
Conversation
Thanks for the PR!
If you need to make changes to your pull request, you can update the commit with Thanks again for your help! |
Thanks @caitp! We'll definitely look at it, but you could change the prefix from I did a writeup on Angular+Bower here, if you're interested: http://briantford.com/blog/angular-bower.html |
I don't know how common a use-case it really is, but if it doesn't wind up in core it would probably be a good blag post to inform people about how they could do it, I guess. Anyway it seems to be passing on Travis apart from FF failing to start on SauceLabs, so I'll leave it there. If people decide it's useful, ping me and I'll write up some docs for it. |
Yeah, ignore Travis. I think we'll come back to this after 1.2.0 is out. |
This does not support data- prefix. |
@JDvorak, as far as I'm aware, there's nothing preventing an ng-attr from working like so: <div ng-attr="{'data-toggle=collapse': mobile, 'data-target=#somewidget': mobile}">
...
</div> The attribute toggling doesn't depend on the attribute normalization engine, only the ng-attr attribute is concerned about that at all. However I haven't looked at this in like 2 weeks, so it's quite possible that I am mistaken, and would love to see a demonstration that I'm wrong, if in fact I am! |
But you can't do data-ng-attr. It forces your html to be full of nonstandard attributes. |
I don't believe there is anything preventing you from doing |
#4478 appears to be, from my tests, not correct --- or an invalid bug report. However, if there is in fact a problem with this, it is probably something browser-specific and hard to debug without more information about how to reproduce the issue. |
I'm sorry, but I wasn't able to verify your CLA signature. CLA signature is required for any code contributions to AngularJS. Please sign our CLA and ensure that the CLA signature email address and the email address in this PR's commits match. If you signed the CLA as a corporation, please let me know the company's name. Thanks a bunch! PS: If you signed the CLA in the past then most likely the email addresses don't match. Please sign the CLA again or update the email address in the commit of this PR. |
CLA signature verified! Thank you! Someone from the team will now triage your PR and it will be processed based on the determined priority (doc updates and fixes with tests are prioritized over other changes). |
+1 |
The ngAttr directive allows for attributes to be conditionally applied to an element. It also supports attributes with interpolated values. 1. Simple case: `ng-attr="{checked: isChecked}"` 2. With values: `ng-attr="'data-whatever={{value}}'"` 3. Set of attributes with interpolated values: `ng-attr="['attr1={{a}}', 'attr2={{b()}}']"` 4. Conditional attributes with values: `ng-attr="{'data-whatever=shmee': showWhatever&&!flurp()}"`
+1 |
1 similar comment
+1 |
Use case: providing case-sensitive attributes to svg elements. 👍 (although I didn't check if this PR actually does that 😅) |
+1 |
+1 |
2 similar comments
+1 |
+1 |
@caitp I'm using your patch to achieve this without success: <button ng-attr="{'ng-click=alert()': true}">Call alert()</button> app.controller('AppCtrl', function($scope) {
$scope.alert = function() {
alert('Click!');
};
}); Demo: http://plnkr.co/edit/9ZRWmf?p=preview Do you see why it does not work? ( |
@tkrotoff It's correctly adding the attribute, the event listeners and watchers just aren't being bound interactively. |
@tkrotoff: This patch does not compile directives which are added dynamically, or "un-compile" them when removed, that's a different problem entirely. It's actually very hard to implement that correctly because we don't have a way of implementing destructors in JS, as you would in a language like C++. Regardless, such mechanics have actually been suggested (by me), but it's not clear whether they will/can ever be implemented. |
Thanks, it's pretty clear now. |
+1 |
Please publish this as non-core component in bower/npm. Meta-programing is awesome, but I think this takes it a step too far for a core component. |
Based on ngClass, this directive allows for attributes to be conditionally
applied to an element. It also supports attributes with interpolated values.
ng-attr="{checked: isChecked}"
checked
attribute is added (with no value) when isChecked istruthy,
and removed when isChecked is falsy.
ng-attr="'data-whatever={{value}}'"
value
from scopeng-attr="['attr1={{a}}', 'attr2={{b()}}']"
interesting stuff.
ng-attr="{'data-whatever=shmee': showWhatever&&!flurp()}"
Someone asked about a way to do this the other night in #angularjs, so I wrote
this directive just for fun. I thought I'd submit it just in case anyone felt
it would be useful in any way.
If it feels useful, I'm happy to write up some good documentation for it --
However, I'm not totally sure what the real use cases for this are.