Angular2-组件变量/组件类属性上的两种方式数据绑定? [英] Angular2 - two way databinding on a component variable / component class property?

查看:86
本文介绍了Angular2-组件变量/组件类属性上的两种方式数据绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular2(测试版6)中,我有一个主菜单组件。

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

<mainmenu></mainmenu>

我想将布尔值绑定为宽或窄。所以我就这样:

I want to bind a boolean for wide or narrow. So I made it into this:

<mainmenu [(menuvisible)]="true"></mainmenu>

但是我想要(我认为)是绑定到javascript类属性(因为我可能有其他要绑定的东西,但希望通过在组件中使用单个类来保持整洁)。

But what I want (I think) is to bind to a javascript class property (as I may have other things to bind but want to be tidy by using a single class in the component).

我收到错误


例外:模板解析错误:无效的属性名称
'menumodel.visible'(

EXCEPTION: Template parse errors: Invalid property name 'menumodel.visible' ("

] [(menumodel.visible)] = menumodel.visible>

][(menumodel.visible)]="menumodel.visible">

如果我尝试使用单个变量而不是类,则得到:

If I try the same with a single variable instead of a class I get:


模板解析错误:解析器错误:意外的令牌'='

Template parse errors: Parser Error: Unexpected token '='

但是(单向绑定?)似乎有效(但我可能想触发菜单从另一个组件转到宽/窄,因此认为这应该是双向数据绑定属性):

However this (one way binding?) does seem to work (but I might want to trigger the menu to go wide/narrow from another component so felt this should be a two-way data bound property):

<menu [vis]="true"></menu>

这是我菜单的一部分:

@Component({
    selector: 'menu',
    templateUrl: './app/menu.html',
    providers: [HTTP_PROVIDERS, ApplicationService],
    directives: [ROUTER_DIRECTIVES, FORM_DIRECTIVES, NgClass, NgForm]
})
export class MenuComponent implements OnInit {

    mainmenu: MainMenuVM;

    constructor(private _applicationService: ApplicationService) {
        this.mainmenu = new MainMenuVM();
    }

    // ...ngOnInit, various functions

}

这是我的MainMenu视图模型类

Here is my MainMenu View Model class

export class MainMenuVM {
    public visible: boolean;
    constructor(
    ) { this.visible = true; }
}

我正在尝试创建一个包含图标和文本的菜单,但是可以缩小以仅显示图标。我将向上发出此事件给父组件,以更改菜单旁边容器的位置。触发内容容器最大化将触发菜单变窄-我并不是说这是最好的方法,但是我想在深入探讨之前解决此特定问题。

I'm trying to create a menu which has icons and text, but can go narrow to just show icons. I will emit this event upwards to a parent component to alter the position of the container next to the menu. Triggering a content container to maximised will trigger the menu to go narrow - I am not saying this is the best way, but I would like to resolve this particular question before going deeper.

请注意:我这里没有数据绑定到输入控件-只是数据绑定到了一个组件,因此我可以修改UI。

Please note: I am not databinding to an input control here - just databinding to a component so I can then modify the UI.

这是从Angular速查表

This is from the Angular cheatsheet

<my-cmp [(title)]="name">   
Sets up two-way data binding. Equivalent to: <my-cmp [title]="name" (titleChange)="name=$event">

预先感谢!

UPDATE

集成接受的答案中的代码,并在此处适应我的特定用例,最后得到有效的代码:

Integrating the code from the accepted answer and adapting for my particular use case here the final working code:

app.html

app.html

...header html content

// This is what I started with
<!--<menu [menuvisible]="true" (menuvisibleChange)="menuvisible=$event"></menu>-->

// This is two way data binding
// 1. Banana-in-a-box is the input parameter
// 2. Banana-in-a-box is also the output parameter name (Angular appends it's usage with Change in code - to follow shortly)
// 3. Banana-in-a-box is the short hand way to declare the commented out code
// 4. First parameter (BIAB) refers to the child component, the second refers the variable it will store the result into.
// 5. If you just need an input use the remmed out code with just the first attribute / value
<menu [(menuvisible)]="menuvisible"></menu>

.. div content start 
<router-outlet></router-outlet>
.. div content end 

app.component.ts( root)

app.component.ts (root)

export class AppComponent implements OnInit{
   menuvisible: Boolean;
}

menu.component.ts(root的子代)

menu.component.ts (child of root)

export class MenuComponent implements OnInit {
    // Parameters - notice the appending of "Change"
    @Input() menuvisible: boolean;
    @Output() menuvisibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();

    // Init
    ngOnInit() {
        // Populate menu - fetch application list       
        this.getApplications();

        // Initially we want to show/hide the menu depending on the input parameter
        (this.menuvisible === true) ? this.showMenu() : this.hideMenu();
    }

    //...more code
}

menu.html

menu.html

<div id="menu" [ngClass]="menuStateClass" style="position: absolute; top:0px; left: 0px;z-index: 800; height: 100%; color: #fff; background-color: #282d32">
    <div style="margin-top: 35px; padding: 5px 0px 5px 0px;">

        <ul class="menuList" style="overflow-x: hidden;">
            <li>IsMenuVisible:{{menuvisible}}</li>
            <li style="border-bottom: 1px solid #3d4247"><a (click)="toggleMenu()"><i class="fa fa-bars menuIcon" style="color: white; font-size: 16px;"></i></a></li>
            <li *ngFor="#app of applications">
                <a [routerLink]="[app.routerLink]">
                    <i class="menuIcon" [ngClass]="app.icon" [style.color]="app.iconColour" style="color: white;"></i>
                    <span [hidden]="menuStateTextHidden">{{ app.name }}</span>
                </a>
            </li>
        </ul>

    </div>
</div>

记住要导入的内容,例如

Remember to import what you need e.g.



'angular2 / core'导入{Component,EventEmitter,OnInit,Input,Output};

import {Component, EventEmitter, OnInit, Input, Output} from 'angular2/core';

在You Tube上强烈推荐此视频:
Angular 2教程(2016)-输入和输出

Highly recommend this video on You Tube: Angular 2 Tutorial (2016) - Inputs and Outputs

推荐答案

对于双向绑定,您需要以下内容:

For two-way binding you need something like:

@Component({
    selector: 'menu',
    template: `
<button (click)="menuvisible = !menuvisible; menuvisibleChange.emit(menuvisible)">toggle</button>
<!-- or 
   <button (click)="toggleVisible()">toggle</button> -->
`,
    // 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<boolean> = new EventEmitter<boolean>();

    // toggleVisible() {
    //   this.menuvisible = !this.menuvisible;       
    //   this.menuvisibleChange.emit(this.menuvisible);
    // }
}

并像使用它

@Component({
  selector: 'some-component',
  template: `
<menu [(menuvisible)]="menuVisibleInParent"></menu>
<div>visible: {{menuVisibleInParent}}</div>
`
  directives: [MenuComponent]
})
class SomeComponent {
  menuVisibleInParent: boolean;
}

这篇关于Angular2-组件变量/组件类属性上的两种方式数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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