Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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
Angular 如何从指令中读取组件变量?_Angular_Angular2 Directives_Angular2 Components - Fatal编程技术网

Angular 如何从指令中读取组件变量?

Angular 如何从指令中读取组件变量?,angular,angular2-directives,angular2-components,Angular,Angular2 Directives,Angular2 Components,我正在开发Angular 2(rc-4)指令,该指令将检查组件是否可以在当前用户权限下查看,并相应地显示/隐藏组件 我在初始化组件时创建了组件标识符。例如,在profile.component.ts中,我正在为ProfileComponent生成id(elementId:string;) 我创建了以下结构指令 import{Directive,Input,TemplateRef,ViewContainerRef}来自“@angular/core”; @指示({ 选择器:“[restrict]”

我正在开发Angular 2(rc-4)指令,该指令将检查组件是否可以在当前用户权限下查看,并相应地显示/隐藏组件

我在初始化组件时创建了组件标识符。例如,在profile.component.ts中,我正在为ProfileComponent生成id(
elementId:string;

我创建了以下结构指令

import{Directive,Input,TemplateRef,ViewContainerRef}来自“@angular/core”;
@指示({
选择器:“[restrict]”
})
出口类限制指令{
authMatrix:任意={
“客人”:{
“app.dashboard.calendar”:false,
“app.dashboard.profile”:false
},
“用户”:{
“app.dashboard.calendar”:false,
“app.dashboard.profile”:真
},
“管理员”:{
“app.dashboard.calendar”:true,
“app.dashboard.profile”:真
}
};
建造师(
私有templateRef:templateRef,
私有viewContainerRef:viewContainerRef
) {}
@Input()设置限制(elementId:string){
让用户:string=“admin”//TODO获取当前登录用户
let grant:boolean=this.authMatrix[user][elementId];//TODO用户权限服务
如果(!授予){
this.viewContainerRef.createEmbeddedView(this.templateRef);
}否则{
this.viewContainerRef.clear();
}
}
}
因此,为了阻止或允许一个组件,我需要将该组件的id传递给指令。目前,我已经将组件id(“app.dashboard.profile”)硬编码到指令输入中,如下所示:

<app-profile *restrict="'app.dashboard.profile'" [parent-id]="elementId" ></app-profile>


此设置有效。但是我想从相关组件的变量中获取组件id,而不是在html标记中硬编码。有没有一种方法可以从指令(RestrictDirective)访问组件(ProfileComponent)内部的变量?

看起来很奇怪:
[parent id]=“elementId
parent id
elementId
都是
ProfileComponent
的属性?我不知道你想从哪里传递什么和传递到哪里。我试图在创建ID时为所有组件保留一个公共结构。所有组件都有parentId和elementId变量。这里不清楚的是,我正在将仪表板组件(ProfileComponent的父组件)的
elementId
变量绑定到ProfileComponent的输入变量
parentId
('parent-id')
profile.parentId您可以将
DashboardComponent
注入
ProfileComponent
并从中读取
elementId
,但这会产生紧密耦合。我想没有别的办法了。谢谢你的回复。注入组件在我的场景中不起作用,因为组件在多个位置重用。这是我假设的。我不认为还有其他方法。这看起来很奇怪:
[parent id]=“elementId
parent id
elementId
都是
ProfileComponent
的属性?我不知道你想从哪里传递什么和传递到哪里。我试图在创建ID时为所有组件保留一个公共结构。所有组件都有parentId和elementId变量。这里不清楚的是,我正在将仪表板组件(ProfileComponent的父组件)的
elementId
变量绑定到ProfileComponent的输入变量
parentId
('parent-id')
profile.parentId您可以将
DashboardComponent
注入
ProfileComponent
并从中读取
elementId
,但这会产生紧密耦合。我想没有别的办法了。谢谢你的回复。注入组件在我的场景中不起作用,因为组件在多个位置重用。这是我假设的。我想没有别的办法了。
import {Directive, Input, TemplateRef, ViewContainerRef} from "@angular/core";

@Directive({
  selector: '[restrict]'
})
export class RestrictDirective {

  authMatrix:any = {
    "guest": {
      "app.dashboard.calendar": false,
      "app.dashboard.profile": false
    },
    "user": {
      "app.dashboard.calendar": false,
      "app.dashboard.profile": true
    },
    "admin": {
      "app.dashboard.calendar": true,
      "app.dashboard.profile": true
    }
  };

  constructor(
    private templateRef: TemplateRef<any>,
    private viewContainerRef: ViewContainerRef
  ) {}

  @Input() set restrict(elementId: string) {
    let user:string = "admin"; // TODO get current logged in user
    let grant:boolean = this.authMatrix[user][elementId]; // TODO user authority service

    if (!grant) {
      this.viewContainerRef.createEmbeddedView(this.templateRef);
    } else {
      this.viewContainerRef.clear();
    }
  }
}
<app-profile *restrict="'app.dashboard.profile'" [parent-id]="elementId" ></app-profile>