-
Notifications
You must be signed in to change notification settings - Fork 27.3k
feat(ngRemove): directive to remove a portion of the DOM tree (similar to ngHide) #2312
Conversation
This isnt two way binding isnt it? Why would I use this over ui-if from Angular UI? |
There isn't two way binding -- it's a very simple directive that just removes the element from the DOM. |
As I understand, there is no possibility to bring removed element back? That can cause problems: for example, ngResource returns empty object before it fetch action object from backend. So if you have something like
this directive will remove this DIV without waiting until user would be loaded. |
@olostan Agreed. That was my point about it requiring two way data-binding. |
Thanks @ganarajpr and @olostan for the feedback. I ported the implementation from
|
@OrenAvissar But that is not a port of ui-if. That is a complete copy? I dont know why they havent moved ui-if to angular core.. But there might be a reason.. |
@ganarajpr Semantics aside, the purpose of this request is to create a starting point for the possibility of adding this functionality to angular, which may or may not be the final implementation. I think @tigbro did a fine job -- he gets all the credit. |
Hi, |
I'd really like to have an |
This implementation looks good to me. I think we should add it to the core, as it's more useful than ng-switch to me. Changing ng-hide/ng-show to remove the element from DOM is possible, but when removing the element, we should remove the scope as well (to save watchers). In order to do scope removing, ng-show/ng-hide has to create a new scope and that would totally break existing apps. Should we drop off ng-switch ? Because really ng-switch is just a different syntax of this thing. |
I think dropping off ng-switch wouldnt be backward compatible. But you could mark it as deprecated or something. Having said that, I think ng-switch and ng-if are akin to switch and if in programming languages. Though one can use one of them to get the effect of another they are both used in different situations? So maybe there is a space for both? |
+1 for merging this after these changes:
|
|
|
I think @IgorMinar, @vojtajina I'll work on those changes tonight. I signed the CLA when I submitted the pull request. I'll go through it again -- maybe I missed something. |
I renamed the directive to I noticed that similar directives have support for I also noted in the docs that besides creating a new scope the original compiled state is used whenever elements are recreated, so any DOM changes made after compilation will be lost when an element is removed. I also added a test case for this. I'm still kind of new to git, so I can use some help for squashing the branch to a single commit without totally messing things up :). Would the best way be to clone the latest version of angular, apply the changes for ngIf, and then do a force push to this branch? I'm the only one working on this branch so reseting shouldn't be a problem. Please let me know if I need to submit the CLA again. |
Awesome @OrenAvissar you don't need to sign the CLA multiple times. |
* | ||
* Note that **when an element is removed using ngIf its scope is destroyed** and **a new scope | ||
* is created when the element is restored**. Also, `ngIf` recreates elements using their | ||
* compliled state. For example, if you use jQuery to add a class to an element after it's |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo compliled -> compiled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll fix the typo and update the commit.
Apart from the comments on the documentation this looks good to merge, yes? |
👍 |
… (adapted from ui-if in AngularUI)
I updated the docs and included a section about binding to primitives in the parent scope. Hopefully it's clear. The link recommended by @petebacondarwin explains it well so I also linked it in the docs. |
Landed as: 2f96fbd! Thanks Oren |
Awesome, love this! |
Great, thank you to everyone for contributing! |
I added a directive called
ngRemove
that I think could be useful. This works similar tongHide
except instead of setting thedisplay: none
css it will remove the element from the DOM.There is already a comment in the code about refactoring
ngHide
"//TODO(misko): refactor to remove element from the DOM". It seems to mengShow/ngHide
imply setting visibility rather than removing an element.Some example cases when
ngRemove
is helpful:When using bootstrap in the situation shown in the HTML snippet below the bootstrap css uses selectors like
.btn-group > .btn:first-child
and.btn-group > .btn:last-child
to properly format a series of buttons. In this example if you just hide the last button#btnCategories
usingdisplay: none
the previous button#btnUpload
won't be styled correctly -- the#btnCategories
button needs to be removed from the DOM to trigger the:last-child
selector on the#btnUpload
button.When using alternate inputs in a form (depending on application settings) it seems cleaner to remove the unused alternates instead of hiding them (in some cases they may share the same
name
orid
attributes). As an example, the code below will useradio
inputs if the application is set to only allow a single selection andcheckbox
inputs if multiple selections are allowed.