【问题标题】:ngFor repeated components radio buttons scope is no isolatedngFor 重复组件单选按钮范围不是孤立的
【发布时间】:2017-12-16 02:52:40
【问题描述】:

我有两个组件,一个是 Post:

import {Component} from '@angular/core';

@Component({
    selector: 'post',
    template: `
    <h1>{{title}}</h1>
    <p>{{postText}}</p>
    <ul>
      <li *ngFor="let postData of posts">
         <news-feed></news-feed>
      </li>
     </ul>
    `
})
export class Post {
    title : string;
    postText : string;
    posts = [{title:"Post1",postText:"Wow greate post"}, {title:"Post1",postText:"Wow greate post"}]
    constructor(title:string, postText:string) {
        this.title  = title;
        this.postText = postText;
    }
}

另一个是新闻源:

    import {Component} from '@angular/core';

    @Component({
        selector: 'news-feed',
        template: `
        <h1>News Feed</h1>
        <div class="radios">
           <div class="form-check form-check-inline">
              <label class="form-check-label">
                 <input class="form-check-input" type="radio" value="success" [(ngModel)]="commandResult.type (change)="getCommandResult()"> Succeeded
              </label>
           </div>
           <div class="form-check form-check-inline">
              <label class="form-check-label">
                 <input class="form-check-input" type="radio" value="fail [(ngModel)]="commandResult.type (change)="getCommandResult()"> Failed
             </label>
           </div>
         </div>`
    })
    export class NewsFeed {
        commandResult: any = {
          type: 'fail'
        };
        constructor() {
        }
        getCommandResult() {}
    }

我在帖子组件中重复新闻提要组件,当我更改其中一个重复组件中的单选按钮时,我看到它在所有重复组件中都发生了变化。由于我是 ang2 的新手,所以我可能以错误的方式处理这个问题。任何帮助表示赞赏。 In this link, I think I am facing similar issue, I know its angular1 but still the same issue.

【问题讨论】:

  • 你需要了解父子组件交互。 Angular 文档是一个很好的来源
  • @Vega 你能告诉我我在这里做错了什么吗?我查看了文档,但找不到任何相关信息。

标签: javascript angular angular2-directives angular-components


【解决方案1】:

在父组件类中:

export class Post {
    title : string;
    postText : string;
    posts = [{type:1, title:"Post1",postText:"Wow greate post"}, {type: 0, title:"Post1",postText:"Wow greate post"}]
....

父模板:

   <li *ngFor="let postData of posts">
         <news-feed [commandResult]=postData></news-feed>
    </li>

在子组件类中:

import{Input) from "@angular/core"

export class NewsFeed {
      @Input() commandResult: any ;
      .......

      setCommandResult(value): void {
          this.commandResult.type = value;
      }
 }

还有

  {{ commandResult.title }}
  <input type="radio" (change)="setCommandResult(1)" [value]="1" [checked]="commandResult.type===1">Succeeded
  <input type="radio" (change)="setCommandResult(2)" [value]="2" [checked]="commandResult.type===2">Failed

还有here一些阅读

【讨论】:

  • &lt;news-feed [data]=postData&gt;&lt;/news-feed&gt; 在这一行中,你的意思是&lt;news-feed [commandResult]=postData&gt;&lt;/news-feed&gt; as commandResult 是输入参数吗??
  • 我检查了每个对象,commandResult.type 在每个对象中都不同,但是每当我切换单选按钮时,UI 上所有重复的单选都会翻转。看起来 ngModel 有问题。有什么见解吗?
  • 我已附上问题中的图片
  • 它甚至不起作用,ngModel 甚至没有改变。
  • 检查两个单选按钮。我不确定为什么会这样。
猜你喜欢
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 2011-04-28
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多