Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用angular2在其主控制器中读取指令模型值_Angular - Fatal编程技术网

如何使用angular2在其主控制器中读取指令模型值

如何使用angular2在其主控制器中读取指令模型值,angular,Angular,我有个人页面与指令 <person-directive *ngFor="let person of persons; #idx = index" (remove) = "removePerson(idx)"> </person-directive> } person.ts的定义如下 export class PersonInvolvedComponent { persons: Array<PersonDirective> = [

我有个人页面与指令

    <person-directive *ngFor="let person of persons; #idx = index" (remove) = "removePerson(idx)">
    </person-directive>
}

person.ts的定义如下

 export class PersonInvolvedComponent { 
     persons: Array<PersonDirective> = [];
     constructor( private router: Router, 
                 private _globalService:GlobalService) {
         this.persons.push(new PersonDirective());
 }

}
导出类PersonInvolvedComponent{
人员:数组=[];
构造函数(专用路由器:路由器、,
专用_globalService:globalService){
this.persons.push(new PersonDirective());
}
}

我可以访问directive.ts文件中的这些输入值,但不能访问PersonInvolvedComponent.ts文件中的输入值。我只能看到“personData”对象,但看不到添加到其中的实际输入值。我怎样才能访问它。请推荐我

我认为在angular-2中,不可能在指令的帮助下呈现html,因此您可以使用该组件来呈现和执行一些工作

要实现上述场景,请按照以下步骤操作

  • 使用@component decorator创建组件,并将选择器的值指定为person
    @组件({
    moduleId:module.id,
    选择器:'人',
    templateUrl:“./person.html”
    })
    导出类PersonInvolvedComponent{
    //做必要的事情
    }
  • 复制person detaila所需的HTML代码并粘贴到person.HTML文件中
  • 现在根据需要使用选择器(人员)

    <div *for="let person of personDetails">
       <person [neededInput]="person" (output)="neederStuffs()" > </person>
    </div>
    
    
    

  • 尝试此操作,如果您有任何疑问,请回复我在应用以下更改后检查:

    //个人页面更改

    <person-directive *ngFor="let person of persons; #idx = index" (remove)="removePerson(idx)" [data]="person" ></person-directive>
    
    //person-directive.html

    <div class="group">
      <label class="person-involved-name">Name</label>
      <div class="textContainer"><input type="text" value="" name="nameSelect_1" id="nameSelect_1" [(ngModel)]="data.nameSelect_1" class="txt-box"></div>
    </div>
    <div class="group">
      <label class="person-involved-aliasname">Alias Name</label>
      <div class="textContainer"><input type="text" value="" name="aliasNameSelect_1" id="aliasNameSelect_1" [(ngModel)]="data.aliasNameSelect_1" class="txt-box"></div>
      <div><br class="clr"></div>
    </div>
    
    
    名称
    别名
    

    指令意味着它只是一个组件。。在我的案例中,很多人都做了详细的分析。我想访问personDetail组件中的每个人输入值。我在personComponent中访问的值也需要在personDetails组件中访问。我可以访问对象个人信息,但不能访问添加到其中的输入值。[neededInput]=“person”,(output)=“neederStuffs()”会做什么?[]-表示从父组件到子组件()的输入值,与[personData]=“I”类似,但如果我访问组件中的personData,我会得到空对象。。我可以使用this.person.personData访问。但是它没有显示任何输入字段或值,我的typedexample-
    上面的代码是父组件,现在是子组件<代码>@component({moduleId:module.id,选择器:'person',templateUrl:'./person.html'})导出类PersonInvolvedComponent{@input()详细信息:详细信息;@output()deleteUser;deletePerson(){this.deleteUser.emit();}您能为
    personDirective.ts
    person.ts
    发布您的完整代码吗?不起作用。。我想要子类值。此名称和别名将在添加一个子项后更改。需要维护阵列。我怎样才能做到呢
    <person-directive *ngFor="let person of persons; #idx = index" (remove)="removePerson(idx)" [data]="person" ></person-directive>
    
    import { Component, Input } from '@angular/core';
    
    @Component({
      selector   : 'person-directive',
      templateUrl: './person-directive.html'
    })
    export class PersonDirective {
      @Input() public data: any;      
    }
    
    <div class="group">
      <label class="person-involved-name">Name</label>
      <div class="textContainer"><input type="text" value="" name="nameSelect_1" id="nameSelect_1" [(ngModel)]="data.nameSelect_1" class="txt-box"></div>
    </div>
    <div class="group">
      <label class="person-involved-aliasname">Alias Name</label>
      <div class="textContainer"><input type="text" value="" name="aliasNameSelect_1" id="aliasNameSelect_1" [(ngModel)]="data.aliasNameSelect_1" class="txt-box"></div>
      <div><br class="clr"></div>
    </div>