创建ngSwitch新视图后,Angular2调用自定义函数

时间:2016-04-05 13:23:59

标签: javascript google-maps angular ionic2

我正在使用 Angular2+Ionic2 创建一个小应用。在此应用中,当用户使用 google-map

切换到view segment时,我希望将map-segment初始化为ion-segment

以下是示例代码结构:

<ion-navbar *navbar class="hide-navbar">
    <ion-segment [(ngModel)]="homeSegment">
        <ion-segment-button value="list">
            List..
        </ion-segment-button>
        <ion-segment-button value="map" >
            Map
        </ion-segment-button>
    </ion-segment>
</ion-navbar>
<ion-content>
    <div [ngSwitch]="homeSegment" >

        <div *ngSwitchWhen="'map'">
            <div id="googleMap"></div>
        </div>

        <ion-list *ngSwitchWhen="'list'">
           Listing
        </ion-list>
    </div>
</ion-content>

我尝试为 click 提供 ion-segment-button 听众,但这并没有成功。 div id="googleMap" 附加到 DOM 之前,会触发添加地图的功能,结果是 undefined error

那么,当 ngSwitch 发生时,我们如何理解何时加载新元素?什么是最好的方法呢?

更新(添加js代码)

import {Page, NavController} from 'ionic-angular';
import {DataServiceManager} from '../../services/dataServicesManager';

@Page({
  templateUrl: 'build/pages/listFob/listFob.html'
})
export class ListFob {
    static get parameters(){
        return [[NavController],[DataServiceManager]];
    }
    onPageWillEnter(){
        this.fetchFobs();
    }
    ngOnInit(){
        console.log("on init");
        console.log(this.homeSegment);
    }
    ngAfterContentChecked() {
        console.log("content checked")
    }
    constructor(nav, dataServiceManager){
        this.nav = nav;
        this.dataServiceManager = dataServiceManager;
        this.homeSegment = "list";
    }
    loadMap(){
        console.log(document.getElementById("googleMapAllFobs"));
        // TODO: load map functionality
    }
}

添加指令时出现错误消息

./app/directives/switch-segment.js
Module build failed: SyntaxError: /Users/Piccaza/ionic-projects/learning/fob/app/directives/switch-segment.js: Unexpected token (8:27)
   6 | })
   7 | export class SwitchSegment {
>  8 |     @Output() switchSegment: EventEmitter = new EventEmitter();
     |                            ^
   9 |     ngOnInit() {
  10 |         console.log("Directive triggered!");
  11 |         this.onCreate.emit('dummy');
    at Parser.pp.raise (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:1378:13)
    at Parser.pp.unexpected (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:2817:8)
    at Parser.pp.expect (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:2811:33)
    at Parser.pp.parseMethod (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:1091:8)
    at Parser.pp.parseClassMethod (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:2495:8)
    at Parser.pp.parseClassBody (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:2456:10)
    at Parser.pp.parseClass (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:2339:8)
    at Parser.pp.parseStatement (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:1813:19)
    at Parser.pp.parseExportDeclaration (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:2578:15)
    at Parser.pp.parseExport (/Users/Piccaza/ionic-projects/learning/fob/node_modules/babylon/index.js:2566:29)
 @ ./app/pages/listFob/listFob.js 16:21-63 (CLI v2.0.0-beta.19)

Your system information:

Cordova CLI: Not installed
Ionic Version: 2.0.0-beta.3
Ionic CLI Version: 2.0.0-beta.19
Ionic App Lib Version: 2.0.0-beta.9
ios-deploy version: 1.8.5 
ios-sim version: 5.0.6 
OS: Mac OS X El Capitan
Node Version: v5.3.0
Xcode version: Xcode 7.2.1 Build version 7C1002 

3 个答案:

答案 0 :(得分:4)

当更改离子段时,您也可以调用函数。

var depKeyArrLen = depKeyArr.length;

for (var j = 0; j < depKeyArrLen; j++) {
   depKeyArr.push("whatever");
}

并在您的组件中添加类似

的功能
<ion-segment [(ngModel)]="homeSegment" (ionChange)="updatePage(homeSegment)">

也许这有助于某人。

答案 1 :(得分:3)

ngSwitch添加/删除元素时,没有内置支持调用函数。

我会创建一个在创建时调用该函数的指令(例如在ngOnInit()中)或发出一个可以绑定事件处理程序的事件,并将其应用于{{ngSwitch时添加的组件。 1}}分支已启用。

如果ngSwitch添加了一个组件,您也可以在此组件中实现它(取决于您的要求)

<强>更新

    <ion-list (onCreate)="doSomething()" *ngSwitchCase="'list'">
       Listing
    </ion-list>
@Directive(selector: '[onCreate]')
export class OnCreate implements OnInit {
  @Output() onCreate:EventEmitter = new EventEmitter();

  ngOnInit() {
    this.onCreate.emit('dummy');
  }
}

Plunker example

答案 2 :(得分:0)

最后我通过在 (change)="onSegmentChanged($event)" 上提供 ion-segment element 来解决这个问题。

只要 ion-segment switch,就会触发。当我切换到 dom element with id googleMapAllFobs 时,我可以访问value="map"

在调用onSegmentChanged函数之前提供了 load google map 功能的检查,以确保当前视图包含要加载谷歌地图的div。

对于当前的要求看起来很容易。