首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular2可选隔离范围(从角度1)

Angular2可选隔离范围(从角度1)
EN

Stack Overflow用户
提问于 2017-01-02 07:12:56
回答 1查看 1.1K关注 0票数 0

我研究了角2。对于示例组件,需要一个用户参数来呈现有关该用户的信息:

代码语言:javascript
复制
<user-profile [user]="currentUser"></user-profile>

我想在多个地方使用这个组件/如何在组件‘用户配置文件’中进行隔离绑定?

更新,例如,我使用组件

代码语言:javascript
复制
@Component({
selector: 'my-hero-detail',

template: `

<div *ngIf="hero">
<h2>{{hero.name}} details!</h2>
<div><label>id: </label>{{hero.id}}</div>
<div>
  <label>name: </label>
  <input [(ngModel)]="hero.name" placeholder="name"/>
</div>
</div>
 `
 })

我在组件中传递数据

代码语言:javascript
复制
<my-hero-detail [hero]="selectedHero"></my-hero-detail>

更改hero.name使更改和selectHero。

如何避免呢?我想selectHero不改变

EN

回答 1

Stack Overflow用户

发布于 2017-08-10 10:49:27

像这样编写您的子组件

代码语言:javascript
复制
import { Component, Input } from '@angular/core';     
    import { User } from './user';
     
    @Component({
      selector: 'user-profile',
      template: `
        <h3>User Name: {{user.name}} says:</h3>
        <p>Telephone: {{user.telephone}}</p>
      `
    })
    export class UserProfile{
      @Input() user: User;
    }

然后构造父级并将用户传递给子级。

代码语言:javascript
复制
import { Component } from '@angular/core';     
    import { User } from './user';
     
    @Component({
      selector: 'user-list',
      template: `
        <user-profile *ngFor="let currentUser of users" [user]="currentUser"></user-profile>
      `
    })
    export class UserListComponent {
      heroes = User[];
    }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41422708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档