bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Alerts ignore ng-hide/ng-show

Open nicstrong opened this issue 13 years ago • 10 comments

If you use ng-show on an alert directive it will not work because it appears to always be overwritten with the closable status.

See this plurk - http://plnkr.co/edit/ZvCs3fcpWUrvJPXEyYHT for a example.

nicstrong avatar May 01 '13 05:05 nicstrong

@nicstrong It is not about the closable property but a scoping issue. Basically the alert directive creates a new isolated scope. In AngularJS one DOM element can have only one scope attached to it so the ng-hide gets "locked" in an isolated scope. You can easily work-around it by doing the following:

<alert ng-hide="$parent.hideAlerts" type="error">Alert with directive</alert>

But I agree that this is not very intuitive. The idea behind the alert directive was that is was mostly data-driven... but people seem to be bumping into this scoping issue over and over again, so we might drop an isolated scope from this directive.

@angular-ui/bootstrap I know, it was discussed before... What is your take on this one?

pkozlowski-opensource avatar May 01 '13 07:05 pkozlowski-opensource

Thanks for the explanation. It is clear now the issue.

Is there a reason for creating the isolated scope?

nicstrong avatar May 01 '13 09:05 nicstrong

@nicstrong yes, usually when a directive provides a template it should have an isolated scope. We had a longish discussion about it in https://github.com/angular-ui/bootstrap/issues/285

Also Misko's video on directives writing (http://www.youtube.com/watch?v=WqmeI5fZcho) has an excellent discussion on it toward the end of the video. I've got an impression that this part is often not well understood

Having said the above I really can see that this might be confusing, especially for alerts... Not sure what the best curse of actions... On one hand I understand that in some practical cases the current solution is not good. At the same time if feels "right". Dunno, I guess practicality should win :-)

pkozlowski-opensource avatar May 01 '13 12:05 pkozlowski-opensource

I'd say a child scope would be better in this case... if what's "right" is making it hard and confusing for most people, then it may not be "right" :rabbit:

ajoslin avatar May 01 '13 14:05 ajoslin

Yeh, it should be practice that determines what is "right" :-) Let's turn this directive into one that uses a child scope then!

pkozlowski-opensource avatar May 01 '13 14:05 pkozlowski-opensource

@nicstrong So, I had a look at this issue, crafted a PR and then we had a lengthy discussion about it (see https://github.com/angular-ui/bootstrap/pull/398). Finally we are coming to the conclusion that isolated scopes are "good thing".

If you want to refer to variables in a parent scope just prefix them with $parent, as I've proposed above.

pkozlowski-opensource avatar May 09 '13 23:05 pkozlowski-opensource

Ok, i am happy with that :+1: with that. At the time I was 1 week into using javascript/angular for the first time. So the whole scoping thing was very confusing to a C++/C#/Java programmer. But I am finally starting to get the hang of things.

nicstrong avatar May 10 '13 00:05 nicstrong

+1, please add this somewhere in the documentation.

chiefy avatar Jun 11 '13 15:06 chiefy

+1 on the >> docs comment.

pankajtandon avatar Aug 02 '13 16:08 pankajtandon

+1 "Yeh, it should be practice that determines what is "right" :-)"

kevinlbatchelor avatar Mar 17 '14 16:03 kevinlbatchelor