ViewChild - Angular 2 Javascript [英] ViewChild - Angular 2 Javascript

查看:24
本文介绍了ViewChild - Angular 2 Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Angular 2 Javascript 中使用 ViewChild?我参考了 angular.io 文档,并尝试使用以下代码但它不起作用.谁能帮我?提前致谢.

ma​​in.html

app.component.js

(function (app) {app.AppComponent = ng.core.成分({选择器:'我的应用',templateUrl: 'app/views/main.html',指令:[ng.router.ROUTER_DIRECTIVES],//<----这里不加载组件视图提供者:[ng.http.HTTP_PROVIDERS],查询:{'viewChild1Component': new ng.core.ViewChild(app.Child1Component)//Parent使用ViewChild调用Child 1},}).班级({构造函数:[ng.router.Router, ng.http.Http, function (router, http) {this.router = 路由器;this.http = http;}],ngAfterViewInit: 函数 () {this.viewChild1Component.onSubmit();},});ng.router.RouteConfig([//在这里加载组件{ path: '/child1', component: app.Child1Component, name: 'Child 1', useAsDefault: true },])(app.AppComponent);})(window.app || (window.app = {}));

child1.js

(function (app) {app.Child1Component = ng.core.成分({选择器:'测试',模板:'<div>Test</div>',}).班级({构造函数:[ng.router.Router,函数(路由器){this.router = 路由器;}],onSubmit: 函数 () {alert('来自家长的来电');},});})(window.app || (window.app = {}));

index.html

<html xmlns="http://www.w3.org/1999/xhtml"><头><!-- 1. 加载库--><script src="scripts/traceur-runtime.js"></script><script src="scripts/system.js"></script><!-- IE 需要 polyfill --><script src="scripts/es6-shim.min.js"></script><script src="scripts/angular2-polyfills.js"></script><script src="scripts/Rx.umd.js"></script><script src="scripts/angular2-all.umd.js"></script><!--组件--><script src="app/components/child1.js"></script><script src="app/components/child2.js"></script><!-- 2. 加载我们的模块"--><script src='app/app.component.js'></script><script src='app/boot.js'></script><!-- 3. 显示应用程序--><身体><my-app>正在加载...</my-app></html>

解决方案

您需要将您的字段定义到组件元数据的 queries 属性中,如下所述:

var AppComponent = ng.core.成分({选择器:应用程序",模板:'

'+' <test></test>'+'</div>',查询:{'subComponent': new ng.core.ViewChild(TestComponent) <-----},指令:[ TestComponent ]}).班级({构造函数:函数(){},ngAfterViewInit:function(){this.subComponent.onSubmit();}});

subComponent 将在 ngAfterViewInit 钩子被调用之前设置.

前面的代码依赖于下面的TestComponent:

var TestComponent = ng.core.Component({选择器:'测试',模板:'<div>测试</div>'}).班级({构造函数:函数(){},提交:函数(){console.log('onSubmit');}});

编辑

就您而言,您可以利用路由.这意味着您的 viewChild1Component 属性将在调用 ngAfterViewInit 方法后设置,因为 Child1Component 组件已加载到 router-出口之后.事实上,事情是动态的...

如果您在之后使用 viewChild1Component 属性(例如单击时),这将起作用...请参阅下面的示例:

app.AppComponent = ng.core.成分({选择器:应用程序",模板:'

'+' <路由器插座></路由器插座>'+' <div (click)="submit()">提交</div>'+'</div>',查询:{'viewChild1Component': new ng.core.ViewChild(Child1Component) },指令:[ng.router.ROUTER_DIRECTIVES]}).班级({提交:函数(){console.log('####提交');this.viewChild1Component.onSubmit();}});

How can I use ViewChild in Angular 2 Javascript? I have referred angular.io docs and I have tried with the following code and it is not working. Can anyone help me? Thanks in advance.

main.html

<router-outlet></router-outlet>

app.component.js

(function (app) {

app.AppComponent = ng.core
        .Component({
            selector: 'my-app',
            templateUrl: 'app/views/main.html',
            directives: [ng.router.ROUTER_DIRECTIVES], //<----not loading components here
            viewProviders: [ng.http.HTTP_PROVIDERS],
            queries: {
                'viewChild1Component': new ng.core.ViewChild(app.Child1Component) //Parent calls Child 1 using ViewChild
            },
        })
        .Class({
            constructor: [ng.router.Router, ng.http.Http, function (router, http) {
                this.router = router;
                this.http = http;

            }],
            ngAfterViewInit: function () {
                this.viewChild1Component.onSubmit();
            },
        });

ng.router
        .RouteConfig([
          //loading components here
          { path: '/child1', component: app.Child1Component, name: 'Child 1', useAsDefault: true }, 
        ])(app.AppComponent);

})(window.app || (window.app = {}));

child1.js

(function (app) {

    app.Child1Component = ng.core
            .Component({
                selector: 'test',
                template: '<div>Test</div>',
            })
            .Class({
                constructor: [ng.router.Router, function (router) {
                    this.router = router;
                }],
                onSubmit: function () {
                    alert('Call from Parent');
                },
            });

})(window.app || (window.app = {}));

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!-- 1. Load libraries -->
    <script src="scripts/traceur-runtime.js"></script>
    <script src="scripts/system.js"></script>
    <!-- IE required polyfill -->
    <script src="scripts/es6-shim.min.js"></script>

    <script src="scripts/angular2-polyfills.js"></script>
    <script src="scripts/Rx.umd.js"></script>
    <script src="scripts/angular2-all.umd.js"></script>

    <!--components-->
    <script src="app/components/child1.js"></script>
    <script src="app/components/child2.js"></script>

    <!-- 2. Load our 'modules' -->
    <script src='app/app.component.js'></script>
    <script src='app/boot.js'></script>

</head>
<!-- 3. Display the application -->
<body>
    <my-app>Loading...</my-app>
</body>
</html>

解决方案

You need to define your field into the queries attribute of your component metadata, as described below:

var AppComponent = ng.core.
  Component({
    selector: "app", 
    template:
      '<div>' +
      '  <test></test>' +
      '</div>',
    queries: {
      'subComponent': new ng.core.ViewChild(TestComponent) <-----
    },
    directives: [ TestComponent ]
  })
  .Class({
    constructor: function() {
    },

    ngAfterViewInit:function(){
      this.subComponent.onSubmit();
    }
  });

The subComponent will set before the ngAfterViewInit hook is called.

The previous code relies on the following TestComponent:

var TestComponent = ng.core.Component({
    selector:'test',
    template: '<div>Test</div>'
  }).Class({
    constructor: function(){
    },

    onSubmit: function(){
      console.log('onSubmit');
    }
  });

Edit

In your case, you leverage routing. This means that your viewChild1Component property will be set after the call of the ngAfterViewInit method since the Child1Component component is loaded in the router-outlet after. Things are dynamic in fact...

If you use the viewChild1Component property after (on a click for example), this will work... See the sample below:

app.AppComponent = ng.core.
  Component({
    selector: "app", 
    template:
     '<div>' +
     '  <router-outlet></router-outlet>' +
     '  <div (click)="submit()">Submit</div>' +
     '</div>',
  queries: { 'viewChild1Component': new ng.core.ViewChild(Child1Component) },
  directives: [ng.router.ROUTER_DIRECTIVES]
})
.Class({
  submit: function() {
    console.log('#### submit');
    this.viewChild1Component.onSubmit();
  }
});

这篇关于ViewChild - Angular 2 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆