我是Angular的新手,我还在努力理解它.我已经按照微软虚拟学院的课程进行了测试,这很棒,但我发现他们所说的内容与我的代码行为之间存在一些差异!具体来说,我试图"将一个组件放在另一个组件中",如下所示:
@Component({
selector: 'parent',
directives: [ChildComponent],
template: `
<h1>Parent Component</h1>
<child></child>
`
})
export class ParentComponent{}
@Component({
selector: 'child',
template: `
<h4>Child Component</h4>
`
})
export class ChildComponent{}
Run Code Online (Sandbox Code Playgroud)
这是他们在课程上做的相同的例子,但在我的代码中不起作用!特别是VisualStudio告诉我,组件装饰器中不存在'directives'属性.我怎么解决这个问题?
我正在创建一个自定义指令来隐藏某些条件下的元素,但它没有按照我在谷歌搜索中找到的指令工作。
所有其他工作正常但元素不影响显示属性
//use of directive
<div manAuthorized [permission]="'permission-string'" class="col-2 data-object-link">
// actual directive
@Directive({
selector: '[manAuthorized]'
})
export class AuthorizedDirective implements OnInit {
@Input() permission: string;
private userObservable: Observable<UserAuthorizations>;
private currentUser: any;
constructor(private elementRef: ElementRef, private configService: ConfigService, private currentUserService: CurrentUserService) {
this.userObservable = this.currentUserService.getCurrentUser();
}
ngOnInit(): void {
this.userObservable.subscribe((user) => {
this.currentUser = user;
if (!this.authorized()) {
this.elementRef.nativeElement.display = 'none';
}
});
}
private authorized() {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)