Angular 2,传递完整的对象作为参数
Angular 2, passing full object as parameter
我正在学习 angular 2,但我遇到了问题。
事实上,实际上,我将每个组件 属性 传递给模板,如下所示:
import {Component, bootstrap, NgFor,NgModel} from 'angular2/angular2';
import {TodoItem} from '../item/todoItem';
@Component({
selector: 'todo-list',
providers: [],
templateUrl: 'app/todo/list/todoList.html',
directives: [NgFor,TodoItem,NgModel],
pipes: [],
styleUrls:['app/todo/list/todoList.css']
})
export class TodoList {
list:Array<Object>;
constructor(){
this.list = [
{title:"Text 1", state:false},
{title:"Text 2", state:true}
];
}
}
<todo-item [title]="item.title" [state]="item.state" *ng-for="#item of list"></todo-item>
import {Component, bootstrap, Input} from 'angular2/angular2';
@Component({
selector: 'todo-item',
providers: [],
templateUrl: 'app/todo/item/todoItem.html',
directives: [],
pipes: [],
styleUrls:['app/todo/item/todoItem.css']
})
export class TodoItem {
@Input()
title:String;
@Input()
state:Boolean;
}
我想知道是否可以通过传递每个 属性 直接在模板内部传递完整对象?
<todo-item [fullObj]="item" *ng-for="#item of list"></todo-item>
是的,将整个对象作为 属性 传递是完全没问题的。
语法相同,所以只需为整个对象创建一个属性。
@Component({
selector: 'my-component'
})
export class MyComponent{
@Input() item;
}
<my-component [item]=item></my-component>
这是一个例子:http://www.syntaxsuccess.com/viewarticle/recursive-treeview-in-angular-2.0
做起来没有问题。您可以选择两种语法:
@Component({
selector: 'my-component',
inputs: ['item: item']
})
export class TodoItem {
item: { title: string, state: boolean };
}
或
@Component({
selector: 'my-component'
})
export class TodoItem {
@Input() item: { title: string, state: boolean };
}
和绑定:
<todo-item [item]="item" *ng-for="#item of list"></todo-item>
你需要记住的一点是,当以这种方式传递一个对象时,你传递的是一个对该对象的引用.这意味着您对 "child" 组件中的对象所做的任何更改都将反映在 "parent" 组件对象中:
export class TodoItem implements OnInit {
ngOnInit() {
//This is modifying the object in "parent" Component,
//as "this.item" holds a reference to the same "parent" object
this.item.title = "Modified title";
}
}
如果您分配不同的对象,则例外。在那种情况下,它不会反映在 "parent" 组件中,因为它不再是同一个对象引用:
export class TodoItem implements OnInit {
ngOnInit() {
//This will not modify the object in "parent" Component,
//as "this.item" is no longer holding the same object reference as the parent
this.item = {
title: 'My new title',
state: false
};
}
}
我正在学习 angular 2,但我遇到了问题。
事实上,实际上,我将每个组件 属性 传递给模板,如下所示:
import {Component, bootstrap, NgFor,NgModel} from 'angular2/angular2';
import {TodoItem} from '../item/todoItem';
@Component({
selector: 'todo-list',
providers: [],
templateUrl: 'app/todo/list/todoList.html',
directives: [NgFor,TodoItem,NgModel],
pipes: [],
styleUrls:['app/todo/list/todoList.css']
})
export class TodoList {
list:Array<Object>;
constructor(){
this.list = [
{title:"Text 1", state:false},
{title:"Text 2", state:true}
];
}
}
<todo-item [title]="item.title" [state]="item.state" *ng-for="#item of list"></todo-item>
import {Component, bootstrap, Input} from 'angular2/angular2';
@Component({
selector: 'todo-item',
providers: [],
templateUrl: 'app/todo/item/todoItem.html',
directives: [],
pipes: [],
styleUrls:['app/todo/item/todoItem.css']
})
export class TodoItem {
@Input()
title:String;
@Input()
state:Boolean;
}
我想知道是否可以通过传递每个 属性 直接在模板内部传递完整对象?
<todo-item [fullObj]="item" *ng-for="#item of list"></todo-item>
是的,将整个对象作为 属性 传递是完全没问题的。
语法相同,所以只需为整个对象创建一个属性。
@Component({
selector: 'my-component'
})
export class MyComponent{
@Input() item;
}
<my-component [item]=item></my-component>
这是一个例子:http://www.syntaxsuccess.com/viewarticle/recursive-treeview-in-angular-2.0
做起来没有问题。您可以选择两种语法:
@Component({
selector: 'my-component',
inputs: ['item: item']
})
export class TodoItem {
item: { title: string, state: boolean };
}
或
@Component({
selector: 'my-component'
})
export class TodoItem {
@Input() item: { title: string, state: boolean };
}
和绑定:
<todo-item [item]="item" *ng-for="#item of list"></todo-item>
你需要记住的一点是,当以这种方式传递一个对象时,你传递的是一个对该对象的引用.这意味着您对 "child" 组件中的对象所做的任何更改都将反映在 "parent" 组件对象中:
export class TodoItem implements OnInit {
ngOnInit() {
//This is modifying the object in "parent" Component,
//as "this.item" holds a reference to the same "parent" object
this.item.title = "Modified title";
}
}
如果您分配不同的对象,则例外。在那种情况下,它不会反映在 "parent" 组件中,因为它不再是同一个对象引用:
export class TodoItem implements OnInit {
ngOnInit() {
//This will not modify the object in "parent" Component,
//as "this.item" is no longer holding the same object reference as the parent
this.item = {
title: 'My new title',
state: false
};
}
}