Angular 自定义 structural 指令的一个例子

简介: Angular 自定义 structural 指令的一个例子

image.pngimage.png

import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
/**
 * Add the template content to the DOM unless the condition is true.
 */
/* The directive's selector is typically the directive's 
attribute name in square brackets, [appUnless]. The brackets define a CSS attribute selector.
*/
@Directive({ selector: '[appUnless]'})
export class UnlessDirective {
  private hasView = false;
  /*
  A simple structural directive like this one creates an
  embedded view from the Angular-generated <ng-template> and
   inserts that view in a view container adjacent to the directive's original <p> host element.
  */
  /*
  You'll acquire the <ng-template> contents with a TemplateRef and access the view container through a ViewContainerRef.
这里不用操心这些服务的 provider 是谁?
  */
  constructor(
    // 访问<ng-template>内容
    private templateRef: TemplateRef<any>,
    // 访问view container
    private viewContainer: ViewContainerRef) { }
    /*
    The directive consumer expects to bind a true/false 
    condition to [appUnless]. That means the directive needs an appUnless property, decorated with @Input
    */
  @Input() set appUnless(condition: boolean) {
    // 如果condition不满足才显示view
    /*
    Angular sets the appUnless property whenever the value of 
    the condition changes. Because the appUnless property does work, it needs a setter.
    */
    if (!condition && !this.hasView) {
      this.viewContainer.createEmbeddedView(this.templateRef);
      this.hasView = true;
    } else if (condition && this.hasView) {
      this.viewContainer.clear();
      this.hasView = false;
    }
  }
}

在App module里的declarations区域导入新建的Directive:image.png在 Component HTML 里消费这个自定义指令:

<h2 id="appUnless">UnlessDirective</h2>
<p>
  The condition is currently
  <span [ngClass]="{ 'a': !condition, 'b': condition, 'unless': true }">{{condition}}</span>.
  <button
    (click)="condition = !condition"
    [ngClass] = "{ 'a': condition, 'b': !condition }" >
    Toggle condition to {{condition ? 'false' : 'true'}}
  </button>
</p>
<!-- 为什么这里消费 Directive 用的是 *? -->
<p *appUnless="condition" class="unless a">
  (A) This paragraph is displayed because the condition is false.
</p>
<p *appUnless="!condition" class="unless b">
  (B) Although the condition is true,
  this paragraph is displayed because appUnless is set to false.
</p>

image.png

相关文章
|
2月前
|
前端开发 开发者 C#
深度解析 Uno Platform 中的 MVVM 模式:从理论到实践的全方位指南,助你轻松掌握通过 C# 与 XAML 构建高效可维护的跨平台应用秘籍
【8月更文挑战第31天】本文详细介绍如何在优秀的跨平台 UI 框架 Uno Platform 中实施 MVVM(Model-View-ViewModel)模式,通过一个简单的待办事项列表应用演示其实现过程。MVVM 模式有助于分离视图层与业务逻辑层,提升代码组织性、易测性和可维护性。Uno Platform 的数据绑定机制使视图与模型间的同步变得高效简便。文章通过构造 `TodoListViewModel` 类及其相关视图,展示了如何解耦视图与模型,实现动态数据绑定及命令处理,从而提高代码质量和开发效率。通过这一模式,开发者能更轻松地构建复杂的跨平台应用。
28 0
|
2月前
|
JavaScript
如何在 Angular 中使用 ViewChild 来访问子组件、指令或 DOM 元素
如何在 Angular 中使用 ViewChild 来访问子组件、指令或 DOM 元素
25 0
|
2月前
|
JavaScript 前端开发
如何在 Angular 中为响应式表单创建自定义验证器
如何在 Angular 中为响应式表单创建自定义验证器
16 0
|
2月前
|
索引
Angular 中的 ngFor 指令
Angular 中的 ngFor 指令
35 0
|
2月前
|
JavaScript
如何在自定义 Angular 指令中使用 @HostBinding 和 @HostListener
如何在自定义 Angular 指令中使用 @HostBinding 和 @HostListener
29 0
|
2月前
在Angular中创建自定义管道
在Angular中创建自定义管道
12 0
|
2月前
|
JavaScript 安全
如何在 Angular Material 中使用自定义 SVG 图标
如何在 Angular Material 中使用自定义 SVG 图标
52 0
|
2月前
|
JavaScript
如何使用 ControlValueAccessor 在 Angular 中创建自定义表单控件
如何使用 ControlValueAccessor 在 Angular 中创建自定义表单控件
14 0
|
5月前
|
JavaScript 前端开发 编译器
Angular 中的结构指令运行时的工作原理
Angular 中的结构指令运行时的工作原理
|
21天前
|
缓存 JavaScript 前端开发
Angular 应用打包和部署
Angular 应用打包和部署
55 1
下一篇
无影云桌面