Angular2 在创建 ngSwitch 新视图后调用自定义函数
Angular2 calling custom function after ngSwitch new view is created
我正在使用Angular2+Ionic2
创建一个小应用程序。在这个应用程序中,我希望当用户使用ion-segment
切换到map-segment
时将google-map
初始化为view 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>
我已经尝试为ion-segment-button
提供click
听众,但这没有成功。在将带有id="googleMap"
的div
附加到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
内置支持在添加/删除元素时调用函数ngSwitch
。
我会创建一个指令,该指令在创建时调用函数(例如在 ngOnInit()
中(或发出事件处理程序可以绑定到的事件,并将其应用于启用 ngSwitch
分支时添加的组件。
如果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');
}
}
普伦克示例
您还可以在更改离子段时调用函数。
<ion-segment [(ngModel)]="homeSegment" (ionChange)="updatePage(homeSegment)">
在您的组件中,像这样的函数
updatePage(homeSegment) {
if (homeSegment === 'map') {
this.loadMap();
}
}
也许这对某人有帮助。
我是如何解决这个问题的,通过提供对ion-segment element
(change)="onSegmentChanged($event)"
.
每当ion-segment
switch
时都会触发.当dom element with id googleMapAllFobs
切换到value="map"
时,我能够访问它。
在调用load google map
函数之前,提供了一个签入onSegmentChanged
函数,以确保当前视图包含div 以加载谷歌地图。
对于当前要求来说看起来很容易。
- 具有自定义函数调用的 HTML 表单
- 使用 jQuery 对动态创建的元素调用自定义函数
- Angular2 在创建 ngSwitch 新视图后调用自定义函数
- 调用插件外部的自定义函数并传递参数
- jQuery:点击事件后调用自定义函数
- 调用自定义数据属性中的函数
- jQuery 在单击时调用自定义函数
- 可能调用的自定义函数列表
- 从firebug控制台调用自定义函数
- 在jQuery事件处理程序中调用自定义函数
- 我没有'我不想两次调用自定义函数
- 当第三方javascript异步函数完成执行时,调用自定义函数
- 是否有一种方法可以在调用对象的未定义函数时调用自定义函数?
- 如何在jqGrid中设置双击函数来调用自定义函数?
- 在java脚本中调用自定义函数而不是getfullyear()
- 从'外部'调用自定义函数中的函数
- 调用自定义函数时发生ReferenceError
- 在d3中调用自定义函数
- 是否可以使用setTimeout Node.js调用自定义函数
- 如何在jquery ajax成功调用之前调用自定义函数