【问题标题】:ng2- component property binding not workingng2-组件属性绑定不起作用
【发布时间】:2016-11-09 22:32:28
【问题描述】:

我正在开发一个 Angular 2 应用程序,其中我创建了一个名为 JobComponent 的组件和一个名为 candidate_types 的属性,并尝试使用属性绑定将属性绑定到 NgbdTypeaheadBasic 组件,但它不起作用,我不知道为什么,下面是文件。

job.module.ts

import { NgbdTypeaheadBasic } from './typehead.component';
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';
import { JobComponent }   from './job.component';
@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ JobComponent, NgbdTypeaheadBasic],
  bootstrap:    [ JobComponent ]
})


export class JobModule { }

job.component.ts

import { Component } from '@angular/core';
import {Http, Response} from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

@Component({
    selector : 'job_block',
    template : "<ngbd-typeahead-basic [options]='candidate_types'></ngbd-typeahead-basic>",
})

export class JobComponent {
    candidate_types:any=['air', 'ocean'];
}

typehead.component.ts

import {Component,Input} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';

@Component({
  selector: 'ngbd-typeahead-basic',
  templateUrl: './partials/third-party/typehead.html'
})
export class NgbdTypeaheadBasic {
  public model: any;

  states:any;

  @Input() options:any;

  constructor() { console.log(this.options); }

  search = (text$: Observable<string>) =>
    text$
      .debounceTime(200)
      .distinctUntilChanged()
      .map(term => term.length < 2 ? []
        : this.states.filter(v => new RegExp(term, 'gi').test(v)).splice(0, 10));
}

typehead.html

<input type="text" class="form-control" [(ngModel)]="model" [ngbTypeahead]="search" />

当我启动应用程序时,所有组件都可以正确渲染,没有任何错误,但我得到的是 undefined for console.log(this.options);

我正在尝试实现 Angular 2 Bootstrap Typehead 组件:

https://ng-bootstrap.github.io/#/components/typeahead

我的参考资料:

https://angular.io/docs/ts/latest/tutorial/toh-pt3.html

【问题讨论】:

  • “不工作”是什么意思?请发布您的错误。
  • 对不起,我搞砸了,让我编辑问题@StefanSvrkota
  • 问题已更新,@StefanSvrkota

标签: twitter-bootstrap angular ng-bootstrap


【解决方案1】:

你得到undefined,因为你试图在constructor 中打印options,并且考虑到它是@Input(),此时它还没有从父组件传递。实现OnInit 并在那里打印,你会看到它会起作用:

import { OnInit } from '@angular/core';

export class NgbdTypeaheadBasic implements OnInit {

ngOnInit() {
console.log(this.options);
}

}

您应该查看Angular 2 Lifecycle Hooks

【讨论】:

  • 工作得很好,谢谢,实际上我不想console.log 的值,它是为了测试,我将把选项分配给也可以正常工作的 states 属性......跨度>
  • 只是一个简单的问题,有没有办法将值从子组件传递到父组件???
  • @AkshayKhale 有,你必须使用EventEmitter 来实现这一点。我建议阅读这篇文章,用最简单的例子解释得很好:themarketingtechnologist.co/…
  • 已经从同一个参考链接实现了,但是它随机停止工作,不知道出了什么问题,再次感谢...
  • @AkshayKhale 如果您遇到任何问题,请发布另一个问题。
猜你喜欢
  • 2010-11-18
  • 1970-01-01
  • 2021-12-01
  • 2012-04-03
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多