1
@Component({
  selector: 'mh-feature-popup',
  template: `
  <div class="full">
  <div>
    <div class="container-fluid" [@featurepop]="state">
      <div class="row">
        <div class="col-xs-12 col-md-4 col-md-offset-4 popup">
          <div class="row">
            <div id="shadow-card">
                <div class="col-xs-12 dialog-header hidden-md hidden-lg">
                    <div class="col-xs-1 skeleton hidden-md hidden-lg clickCursor" (click)="toggleState()">
                        <div class="close"></div>
                    </div>
                    <div class="text_heading col-xs-10">
                        <span *ngIf="name">{{name}}</span>
                    </div>
                </div>
                <div class="row dialog-content">
                    <div class="dialog-title skeleton col-md-10 col-md-push-1 hidden-xs hidden-sm">
                        <span *ngIf="name">{{name}}</span>
                    </div>
                    <div class="col-md-2 skeleton hidden-xs hidden-sm clickCursor" (click)="toggleState()">
                        <div class="close"></div>
                    </div>
                </div>
                <div *ngIf="data" #data_value>{{data}}
                </div>
            </div>
          </div>
        </div> 
      </div>
    </div>
  </div>
</div>
        `,
styles:[`
.full{
    background-color: rgba(0,0,0,0.2);
    width: 100vw;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
}
`],
providers:[ ApiService ],
animations: [
  trigger('featurepop', [
    state('inactive', style({
      transform: 'scale(0)'
    })),
    state('active', style({
      transform: 'scale(1)'
    })),
    transition('inactive => active', [
      animate(250)
    ]),
    transition('active => inactive', [
      animate(250)
    ])
  ])
]
})
 export class FeaturePopUpComponent {
  state = 'inactive';
  @Input()
      data;

  @Input()
      name;

  show(a,b,c){
      this._api.get(a,b,c).subscribe(
          data => {
              this.data = {'data': a};
              this.name = {'name': b};
              console.log(this.data);
          },
          err => console.log(err),
          () => {
              this._zone.run(() => {
                  this.rend.setElementStyle(this.element.nativeElement,"display","block");
                  this.toggle();
                  console.log(this.state);
              });

          }
      );

  }

 }

This is a popup component. So it is supposed to be hidden and when the show() function is called, it is supposed to show the content received from API call.

The show() function is working but the only problem with this is that the data which I get isn't showing in the component(empty popup)enter image description here.

When i change the screen size of browser, the data changes on popupenter image description here.

onChange() work when i change screen size but not when data change. I tried changing data as JSON objects. I tried using changeDetection.Ref and NgZone too but not working. Tried with ngDoCheck() too but isn't working.

I m using angular-universal starter kit. If anyone can make it work please make a jsfiddle or any.

  • Not sure there is the problem but I see one problem - the component modified the input data. input data should be immutable. – Julia Passynkova Apr 20 '17 at 09:37
  • If you find no problem.. plz run it... u'll see it.. and i tried all combinations for this...i tried without input too and input with immutables... – Scien Ce Subject Apr 20 '17 at 10:14
  • please create a plunk and i will try to update to make it work – Julia Passynkova Apr 20 '17 at 10:53
  • @JuliaPassynkova can u help me with making plunk? i tried but i don't know how to make a plunk with angular universal – Scien Ce Subject Apr 22 '17 at 13:45
  • use this template: https://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5?p=catalogue – Julia Passynkova Apr 22 '17 at 14:57
  • https://plnkr.co/edit/TFWLVEhhKDiXDBLz7ztZ?p=preview Lil creepy but i tried my best :) | The data is changed through API calls couldn't replicate that on plunkr. | i can put api calls on either of the component..main or popup... plz take that into consideration. – Scien Ce Subject Apr 23 '17 at 04:48
  • @JuliaPassynkova i can put api calls on either of the component (where ever u prefer is best i'll take ur advice)..main or popup... plunkr in above comment. – Scien Ce Subject Apr 23 '17 at 04:56

1 Answers1

0

Something like will work. I suggest you to read about @Inputs.

Also if you want to display {{name}} in html, you need just define name variable in TS, not data:{name:'aaa"}

here is link to updated plunker: https://plnkr.co/edit/owx397i2mLGBZ46oioQ1?p=preview

//our root app component
import {
  Component,
  NgModule,
  VERSION,
  ViewChild,
  forwardRef,
  Input,
  trigger,
  state,
  style,
  animate,
  transition,
  Inject,
  Renderer,
  ElementRef
} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';


@Component({
  selector: 'feature-popup',
  template: `
    <div class="full">
      <div>
        <div class="container-fluid" [@featurepop]="state">
          <div class="row">
            <div class="col-xs-12 col-md-4 col-md-offset-4 popup">
              <div class="row">
                <div id="shadow-card">
                  <div class="col-xs-12 dialog-header hidden-md hidden-lg">
                    <div class="col-xs-1 skeleton hidden-md hidden-lg clickCursor" (click)="toggle()">
                      <div class="close"></div>
                    </div>
                    <div class="text_heading col-xs-10">
                      <span *ngIf="name">{{name}}</span>
                    </div>
                  </div>
                  <div class="row dialog-content">
                    <div class="dialog-title skeleton col-md-10 col-md-push-1 hidden-xs hidden-sm">
                      <span *ngIf="name">{{name}}</span>
                    </div>
                    <div class="col-md-2 skeleton hidden-xs hidden-sm clickCursor" (click)="toggle()">
                      <div class="close"></div>
                    </div>
                  </div>
                  <div *ngIf="data" #data_value>{{data}}
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  `,
  styles: [`
    .full {
      background-color: rgba(0, 0, 0, 0.2);
      width: 100vw;
      height: 100vh;
      position: fixed;
      top: 0;
      left: 0;
      z-index: 999;
    }

    #shadow-card {
      background-color: white;
      height: 350px;
      box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
      position: absolute;
      top: 50%;
      left: 50%;
      display: inline-block;
      margin-top: 100px;
      margin-left: -200px;
    }
  `],
  providers: [],
  animations: [
    trigger('featurepop', [
      state('inactive', style({
        transform: 'scale(0)'
      })),
      state('active', style({
        transform: 'scale(1)'
      })),
      transition('inactive => active', [
        animate(250)
      ]),
      transition('active => inactive', [
        animate(250)
      ])
    ])
  ]
})
export class FeaturePopUpComponent {
  state = 'inactive';
  data;
  name;

  constructor(private element: ElementRef,
              private rend: Renderer) {
    this.rend.setElementStyle(element.nativeElement, "display", "none");
  }

  show(a, b) {
    this.data = a;
    this.name = b;
    this.rend.setElementStyle(this.element.nativeElement, "display", "block");
    this.toggle();
    console.log(this.state);

  }

  toggle() {
    this.state = (this.state === 'active' ? 'inactive' : 'active');
  }

}

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2 (click)="show()">Open me</h2>
      <feature-popup></feature-popup>
    </div>
  `,
})
export class App {
  @ViewChild(FeaturePopUpComponent) popup: FeaturePopUpComponent;
  a;
  b;

  constructor() {
  }

  show() {
    this.a = 'hi';
    this.b = 'hello';
    this.popup.show(this.a, this.b);
  }
}

@NgModule({
  imports: [BrowserModule, BrowserAnimationsModule],
  declarations: [App, FeaturePopUpComponent],
  bootstrap: [App]
})
export class AppModule {
}
Julia Passynkova
  • 17,256
  • 6
  • 33
  • 32
  • https://plnkr.co/edit/r3FiPp15CMIo7l6gLxQ7?p=preview | Updated the changes but the popup is still empty.. | please have a look | angular-universal doesnt have BrowserAnimationModule | have a look | https://github.com/angular/universal-starter – Scien Ce Subject Apr 25 '17 at 08:09
  • You need to add 'import {BrowserAnimationsModule} from '@angular/platform-browser/animations';' into u planker – Julia Passynkova Apr 25 '17 at 13:26
  • here is working one: https://plnkr.co/edit/228uUVcHZhr6paNGn4Ji?p=preview – Julia Passynkova Apr 25 '17 at 13:27
  • can you help me with question too? http://stackoverflow.com/questions/43559951/cookie-undefined-in-service-cookie-value-exist-in-component-angular/43610363?noredirect=1#comment74305521_43610363 – Scien Ce Subject Apr 29 '17 at 09:54
  • Sure, will take a look. If my solution works for this question, please accept this answer. – Julia Passynkova Apr 29 '17 at 13:25
  • sry to say but thr is no browseranimation module in the current version and i cannot upgrade the current code for newer version. That's why i added the links to git. Still thank you for the help. i'll try to look some other solution – Scien Ce Subject Apr 30 '17 at 18:36