加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

数据绑定在angular2中不起作用

发布时间:2020-12-17 17:22:45 所属栏目:安全 来源:网络整理
导读:@Component({ selector: 'my-content',templateUrl: `./app/content/content.components.html` }) export class ContentComponent { _clickLectre: any; _temoobj:any; private subscription: Subscription; constructor(private commonService: CommonServi
@Component({
      selector: 'my-content',templateUrl: `./app/content/content.components.html`
    })
    export class ContentComponent  { 
      _clickLectre: any;
      _temoobj:any;
      private subscription: Subscription;
      constructor(private commonService: CommonService,private dataService: DataService ) {
      }
      ngOnInit() {               
        this.subscription = this.commonService.notifyObservable$.subscribe((res) => {
          if (res.hasOwnProperty('option') && res.option === 'call_Lecture') {                         
                console.log("call"+res.items);            
                this._clickLectre=res.items;
                console.log("call"+this._clickLectre.facultyname);               
          }
        });
      }
      ngOnDestroy() {
        this.subscription.unsubscribe();
      }

    }

HTML

<tr  *ngIf="_clickLectre">
    <td>Faculty Name : </td>
    <td>{{_clickLectre.facultyname}}</td>
    <td>X</td>
    <td>X</td>
    <td>End Time :</td>
    <td>X </td>
    <td> Present: </td>
    <td>X </td>
   </tr>

我使用commonService来将内容从一个Component传输到另一个Component.

高于this._clickLectre.facultyname值打印在控制台上,但它没有反映在html页面上

为什么数据绑定不起作用是什么问题?

提前致谢

解决方法

由于_clickLectre是异步定义的,你应该使用一个安全的导航操作符(?)

<td>{{_clickLectre?.facultyname}}</td>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读