[英]Angular2 - two way databinding on a component variable / component class property?
在Angular2(Beta 6)中,我有一個主菜單組件。
<mainmenu></mainmenu>
我想為寬或窄綁定一個布爾值。 所以我做到了這個:
<mainmenu [(menuvisible)]="true"></mainmenu>
但我想要的(我認為)是綁定到一個javascript類屬性(因為我可能有其他東西需要綁定,但希望通過在組件中使用單個類來整潔)。
我收到一個錯誤
EXCEPTION:模板解析錯誤:無效的屬性名稱'menumodel.visible'(“
] [(menumodel.visible)] = “menumodel.visible”>
如果我嘗試使用單個變量而不是類,我會得到:
模板解析錯誤:解析器錯誤:意外的標記'='
然而,這(單向綁定?)似乎確實有效(但我可能想觸發菜單從另一個組件擴展/縮小,因此覺得這應該是一個雙向數據綁定屬性):
<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 View Model類
export class MainMenuVM {
public visible: boolean;
constructor(
) { this.visible = true; }
}
我正在嘗試創建一個包含圖標和文本的菜單,但可以縮小到只顯示圖標。 我將向上發送此事件到父組件以更改菜單旁邊的容器位置。 觸發內容容器以最大化將觸發菜單縮小 - 我不是說這是最好的方法,但我想在深入之前解決這個特定的問題。
請注意:我不是在這里數據綁定到輸入控件 - 只是數據綁定到組件,然后我可以修改UI。
這是來自Angular cheatsheet
<my-cmp [(title)]="name">
Sets up two-way data binding. Equivalent to: <my-cmp [title]="name" (titleChange)="name=$event">
提前致謝!
UPDATE
整合接受的答案中的代碼並根據我的特定用例調整最終的工作代碼:
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)
export class AppComponent implements OnInit{
menuvisible: Boolean;
}
menu.component.ts(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
<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>
記得導入你需要的東西,例如
從'angular2 / core'導入{Component,EventEmitter,OnInit,Input,Output};
強烈推薦You Tube上的這個視頻: Angular 2 Tutorial(2016) - 輸入和輸出
對於雙向綁定,您需要以下內容:
@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;
}
我創造了一個短的傻瓜。
您至少有兩種可能性為組件創建雙向數據綁定
V1:使用ngModel Like語法,你必須在@Output屬性名稱的末尾創建@Input屬性和@Input屬性+“更改”的@Vutput屬性
@Input() name : string;
@Output() nameChange = new EventEmitter<string>();
使用V1,您現在可以使用ngModel語法綁定到子組件
[(name)]="firstname"
V2。 只需使用您喜歡的命名創建一個@Input和@Output屬性
@Input() age : string;
@Output() ageChanged = new EventEmitter<string>();
使用V2,您必須創建兩個屬性才能獲得雙向數據綁定
[age]="alter" (ageChanged)="alter = $event"
父組件
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<p>V1 Parentvalue Name: "{{firstname}}"<br/><input [(ngModel)]="firstname" > <br/><br/>
V2 Parentvalue Age: "{{alter}}" <br/><input [(ngModel)]="alter"> <br/><br/>
<my-child [(name)]="firstname" [age]="alter" (ageChanged)="alter = $event"></my-child></p>`
})
export class AppComponent {
firstname = 'Angular';
alter = "18";
}
子組件
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'my-child',
template: `<p>V1 Childvalue Name: "{{name}}"<br/><input [(ngModel)]="name" (keyup)="onNameChanged()"> <br/><br/>
<p>V2 Childvalue Age: "{{age}}"<br/><input [(ngModel)]="age" (keyup)="onAgeChanged()"> <br/></p>`
})
export class ChildComponent {
@Input() name : string;
@Output() nameChange = new EventEmitter<string>();
@Input() age : string;
@Output() ageChanged = new EventEmitter<string>();
public onNameChanged() {
this.nameChange.emit(this.name);
}
public onAgeChanged() {
this.ageChanged.emit(this.age);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.