Внедрить функцию динамически через другой компонент nativescript - PullRequest
0 голосов
/ 13 сентября 2018
here is the component which i show only when an action is called 
```
StackLayout [ngClass]="[msg?.bgColor, 'borderRadius']" width="100%" [height]="msg?.height" *ngIf="isShow && msg?.type===1" (swipe)="onSwipe($event)">
  <Label [text]="msg?.icon" class="icon-square text-center"></Label>
  <Label class="h2 text-center" [ngStyle]="{'color': msg?.fontColor}" [text]="msg?.element1" textWrap="true" horizontalAlignment="center"></Label>
  <Label class="h3 text-center" [ngStyle]="{'color':  msg?.fontColor}" [text]="msg?.element2" textWrap="true" horizontalAlignment="center"></Label>
  <GridLayout *ngIf="msg?.buttons.negativeResponse" columns="*, *" rows="auto, auto">
    <Button col="0" [text]="msg?.element3" width="40%" class="notification-button" (tap)="this[msg?.buttons.positiveResponse]()"></Button>
    <Button col="1" [text]="msg?.element3" width="40%" class="notification-button" (tap)="this[msg?.buttons.negativeResponse]()"></Button>
  </GridLayout>
  <Button *ngIf="!msg?.buttons.negativeResponse" [text]="msg?.element3" class="notification-button" (tap)="this[msg?.buttons.positiveResponse]()"></Button>
  <Label class="horizontal-separator"></Label>
</StackLayout>
```

here is the the main component that when the action is click i pass the data to the above component 
```
this.translateService.get(['transactionpreview.wait1','transactionpreview.wait2','transactionpreview.wait3']).subscribe(t => {
                          this.notificationMsg = (<NotificationData>{
                           element1: t['transactionpreview.wait1'],
                           element2: t['transactionpreview.wait2'],
                           element3: String.fromCharCode(0xf252),
                           element4:  t['transactionpreview.wait3'],
                           buttons: {
                               positiveResponse: myfunction,
                               negativeResponse:  null,
                           },
                           bgColor: 'containerBackgroundColorWhite',
                           fontColor: 'black',
                           position: 'bottom',
                           type: 2,
                           icon: String.fromCharCode(0xf107),
                           height: '40%',
                           timeout: null,
                           clbk: null
                       });
    ```

вот HTML-код, который содержит основной компонент для вызова уведомления <Notification [msg]="notificationMsg"></Notification>

на первом компоненте, когда я нажму (коснусь) = "thismsg? .Buttons.positiveResponse" iхочу вызвать myFuction (), который находится на главном компоненте, а не на компоненте уведомления, как я могу это сделать?

1 Ответ

0 голосов
/ 13 сентября 2018

вместо простого myfunction() {}, определить myfunction как:

myfunction = () => { //do stuff }

ИЛИ передать его с помощью myfunction.bind(this) (см. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)

Затем вы можете сделать:

(tap)="thismsg.buttons.positiveResponse($event)" (событие $ необязательно, вы можете передать все, что захотите)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...