2017-04-20 60 views
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); 
       }); 

      } 
    ); 

    } 

} 

这是一个弹出组件。所以它应该是隐藏的,当show()函数被调用时,它应该显示从API调用接收到的内容。组件DOM没有更新|角通用

show()函数正在工作,但唯一的问题是,我得到的数据没有显示在组件(空弹出)enter image description here

当我改变浏览器的屏幕尺寸,在弹出的enter image description here数据的变化。

onChange()工作时,我改变屏幕大小,但没有数据改变时。我试图将数据更改为JSON对象。我尝试使用changeDetection.Ref和NgZone,但没有工作。用ngDoCheck()也试过,但不起作用。

我使用angular-universal starter kit。如果任何人都可以使它工作,请让一个jsfiddle或任何。

+0

不确定是否有问题,但我看到一个问题 - 组件修改了输入数据,输入数据应该是不可变的 –

+0

如果你没有发现问题,请运行它...你会看到它。 。我尝试了所有的组合...我尝试了没有输入太多,并输入不可变... –

+0

请创建一个plunk,我会尝试更新,使其工作 –

回答

0

类似的东西会工作。我建议你阅读约@Inputs

此外,如果你想显示{{名}}在HTML中,你只需要定义名称变量TS,而不是数据:{名称:'AAA“}

这里是链接更新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 { 
} 
+0

https://plnkr.co/edit/r3FiPp15CMIo7l6gLxQ7?p=preview |更新了更改,但弹出窗口仍然为空请看看|角通用不具有BrowserAnimationModule |看看| https://github.com/angular/universal-starter –

+0

你需要从'@ angular/platform-b​​rowser/animations'添加'import {BrowserAnimationsModule};'进入u planker –

+0

这里是工作之一:https://plnkr.co/edit/228uUVcHZhr6paNGn4Ji?p=preview –