Angular2 - two way databinding on a component variable / component class property?

后端 未结 2 1383
我寻月下人不归
我寻月下人不归 2020-12-24 02:21

In Angular2 (Beta 6) I have a component for a main menu.


I want to bind a boolean for wide or narrow. So

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 03:02

    For two-way binding you need something like:

    @Component({
        selector: 'menu',
        template: `
    
    
    `,
        // HTTP_PROVIDERS should now be imports: [HttpModule] in @NgModule()
        providers: [/*HTTP_PROVIDERS*/, ApplicationService],
        // This should now be added to declarations and imports in @NgModule()
        // imports: [RouterModule, CommonModule, FormsModule]
        directives: [/*ROUTER_DIRECTIVES, FORM_DIRECTIVES, NgClass, NgForm*/]
    })
    export class MenuComponent implements OnInit {
        @Input() menuvisible:boolean;
        @Output() menuvisibleChange:EventEmitter = new EventEmitter();
    
        // toggleVisible() {
        //   this.menuvisible = !this.menuvisible;       
        //   this.menuvisibleChange.emit(this.menuvisible);
        // }
    }
    

    And use it like

    @Component({
      selector: 'some-component',
      template: `
    
    
    visible: {{menuVisibleInParent}}
    ` directives: [MenuComponent] }) class SomeComponent { menuVisibleInParent: boolean; }

提交回复
热议问题