-
Notifications
You must be signed in to change notification settings - Fork 25.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use of local template variable within a *ngIf #6179
Comments
As I see it - the test variable in One fix that I know about is to use the ViewChild annotation and to actually declare the test field:
Plunker link: |
I think this must be a bug with |
@bennylut Then why can we do "#test" if we can't use it in the template?
@kasperpeulen yes hidden is working because it's the default css properties (display: none;) for browsers associated with the hidden bindings, it has no impact on creating/removing the node from the DOM. |
This still doesn't work in RC.3. this works: <div *ngFor="let thing of things">
<span (click)="props.toggle()">
{{thing.name}}
</span>
<div>
<collapse #props>
something
</collapse>
</div>
</div> and this doesn't (even if <div *ngFor="let thing of things">
<span (click)="props.toggle()">
{{thing.name}}
</span>
<div *ngIf="thing.extra">
<collapse #props>
something
</collapse>
</div>
</div> Check out Plunker |
I'm seeing this in rc.4: vaadin/vaadin-grid#411 I really hate to have to use |
Is it right that it used to work in rc.1 and is no longer working in rc.4? |
@RomanGotsiy your plunker works as intended: A variable declared inside of an I.e. this should work.
|
@tbosch I’m not sure why you are closing this. This seems to be a desired functionality (the one outlined in the original post on this issue). |
I guess that I'll have to open a separate issue for my problem mentioned here: #6179 (comment) |
Tried to reproduce my issue in a Plunkr but wasn't able to do so with an Angular 2 component. It seems to only occur with angular2-polymer or vaadin-grid elements. So I've re-opened my issue here: vaadin/vaadin-grid#411 |
@poke Yes, the example in my comment shows the desired behavior and the plunker shows that this is working now. Only the opposite way (i.e. declare a variable inside of |
Can I confirm that the plan/design is for this to never work?
when this does work?
(where y is a variable within app-component that x refers to) It seems sad that we can set the hidden property but not remove the component with *ngIf. |
@lindsaymarkward No, that example does work (see plunkr). What will not work is if the component that declares a reference is within an <div *ngIf="show">
<div *ngIf="x.y">...</div>
</div>
<div *ngIf="show">
<!-- this makes the #x conditional, so is not available to others outside -->
<app-component #x></app-component>
</div> |
Thanks, that's what I meant, actually, but I got it wrong when I typed it. My code example was intended to be like yours with the div surrounding the component. |
Why is this closed? The problem still exists i.e. in the template ...
and in the component class ....
This is a pretty common scenario and means I have to resort to binding to |
I also have this issue, is there a solution yet? |
use |
I have this working now. I'm using it on a video element to control the video among other things. I also need to get the width of the video so I tried the following but it returns 241 when the video is actually more like 600px, is it trying to get the width before the element has fully loaded? ngAfterViewInit() { |
As a solution I found the following. My issue was closest to what what @brendanarnold wrote. I wasn't able to make running the plnkr what @bennylut posted up here. Say you have following template:
Then in your component you "grab" the test element like this:
The setter is called every time the Taken from here: http://stackoverflow.com/a/41095677/336753 |
This one is old, i know, but one could also use Renderer2 (https://angular.io/api/core/Renderer2) like so anywhere in the code: (Setting focus for eg.) TS
HTML
|
Can you guys at least FIX the documentation, because now it is so misleading it makes me cry! https://angular.io/guide/template-syntax#ref-vars
NO you cannot reference a variable anywhere because Angular has contexts within the contexts within...... |
https://angular.io/api/common/NgIf#storing-conditional-result-in-a-variable |
Using openModal type of method where modal has *ngIf condition will set the native element to undefined as the native element is only created when the condition mentioned with *ngIf becomes true. But there is a way around this if you absolutely have to use *ngIf by using Javascript's event loop manipulation using setTimeout or Promises or any sort of callbacks. Here's an example HTML Code
TS Code
Now you won't get the 'nativeElement undefined error' even with *ngIf. Why does this work? It is because of the angular/JS event loop. Basically *ngIf ensures that the modal with the id as 'sampleModal' only comes into the DOM as a native element when the boolean 'showModal' becomes true. Therefore, the command for actually showing the Modal which is the 'showHideModal' function in the given code must be a part of the next loop. While the openModal function is running, the setTimeout will set its calling function at the end of the loop, i.e. after the current function call stack is over. Therefore, we also need to pass in the context of the showHideModal function for it to run in the given scope. So, we use a variable 'that' and set it to the execution context 'this' of the class in which it is defined and pass that.showHideModal in the setTimeout. So, by the time setTimeout executes its callback function, the *ngIf condition has become true and the modal is present in the DOM to be shown or hidden by the 'showHideModal' function. |
@kub1x solution worked for me, but in the case the var is used as an input to another child you should use change detection: <app-child1 #ch1 *ngIf="loadChild"></app-child1>
<app-child2 [brother]="ch1"> </app-child2> @ViewChild('ch1') set ch(ch: Child1Component){
this.ch1 = ch;
this.cdRef.detectChanges(); //update child2's 'brother' input
}
constructor(private cdRef:ChangeDetectorRef){
setTimeout(() => this.loadChild = true, 2000);
} |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Hi,
With the beta 0, with a template like that:
Even if boolValue is true (the input will be visible), test will be "undefined" in the click handler.
The only "fix" someone gave me was to make a directive to autofocus.
The problem that everytime I will need to access a field that may be hidden by a * [embedded template], I won't be able to use it, even if in plain javascript I could test if its value was defined or not.
Plunker link:
http://plnkr.co/edit/JmBBHMo1hXLe4jrbpVdv?p=preview
Is this a "bug" or an expected behaviour?
Is there a more efficient way to deal with this?
The text was updated successfully, but these errors were encountered: